-1

此问题的继续:将仅选中的复选框添加到数据库-PHP

我正在尝试仅将指定的选定复选框添加到我的数据库中。正如该问题中所建议的,我的代码如下所示:

    <html>
<head>


<?php 


// First we execute our common code to connection to the database and start the session
require("common.php");

// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
    // If they are not, we redirect them to the login page.
    header("Location: login.php");

    // Remember that this die statement is absolutely critical.  Without it,
    // people can view your members-only content without logging in.
    die("Redirecting to login.php");
}

// Everything below this point in the file is secured by the login system

// We can display the user's username to them by reading it from the session array.  Remember that because
// a username is user submitted content we must use htmlentities on it before displaying it to the user.
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
?>

</head>
<body>
<body>
 <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="brand" href="#">Notification system</a>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li class="active"><a href="logout.php">Logout</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container">



    </div> 


     <intro>   <b>Hello,</b>
    <u><?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?></u>.</br>
    You are now logged in.
    <u><?php if( $_SESSION['user']['admin'] ==1) echo "you are an admin";
    else echo "you are not an admin"; ?>
    <br /></intro>
<!-- Here I set up a basic html form. When it is submitted, the file done.php is called, and the php method POST, which allows data to be sent.-->
    <form action="done.php" method="POST">
    <!-- This hidden check box is used to check if submit is clicked.-->
     <input type="hidden" name="check_submit" value="1" />
     <!--All the applications are added to an array which we can then use in done.php-->
    <applications><h2>Applications</h2><br><table border="0" cellspacing="10" cellpadding="10">
    <tr>
        <td><b>Application</b></td>
        <td><b>UK</b></td>
        <td><b>Spain</b></td>
        <td><b>France</b></td>
        <td><b>Benelux</b></td>
        <td><b>Germany</b></td>
        <td><b>Sweeden</b></td>
    </tr>
    <tr>
        <td>iManage</td>
        <td><center><input type = "checkbox" name="imanage[0][UK]" value ="UK"/></center></td>
            <td><center><input type = "checkbox" name="imanage[0][SPAIN]" value ="SPAIN"/></center></td>
                <td><center><input type = "checkbox" name="imanage[0][FRANCE]" value ="FRANCE"/></center></td>
                    <td><center><input type = "checkbox" name="imanage[0][BENELUX]" value ="BENELUX"/></center></td>
                        <td><center><input type = "checkbox" name="imanage[0][GERMANY]" value ="GERMANY"/></center></td>
                            <td><center><input type = "checkbox" name="imanage[0][SWEEDEN]" value ="SWEEDEN"/></center></td>
    </tr>
    <tr>
        <td>iReport</td>
        <td><center><input type = "checkbox" name="ireport[]" value="ireportuk"/></center></td>
            <td><center><input type = "checkbox" name="ireport[]" value="ireportspain"/></center></td>
                <td><center><input type = "checkbox" name="ireport[]" value="ireportfrance"/></center></td>
                    <td><center><input type = "checkbox" name="ireport[]" value="ireportbenelux"/></center></td>
                            <td><center><input type = "checkbox" name="ireport[]" value="ireportgermany"/></center></td>
                                    <td><center><input type = "checkbox" name="ireport[]" value="ireportsweeden"/></center></td>

    </tr><tr>
        <td>iQuote</td>
        <td><center><input type = "checkbox" name="iquote[]" value ="iquoteuk"/></center></td>
            <td><center><input type = "checkbox" name="iquote[]" value ="iquotespain"/></center></td>
                <td><center><input type = "checkbox" name="iquote[]" value ="iquotefrance"/></center></td>
                    <td><center><input type = "checkbox" name="iquote[]" value ="iquotebenelux"/></center></td>
                        <td><center><input type = "checkbox" name="iquote[]" value ="iquotegermany"/></center></td>
                            <td><center><input type = "checkbox" name="iquote[]" value ="iquotespain"/></center></td>
    </tr><tr>
        <td>Portal</td>
        <td><center><input type = "checkbox"/ name="portal[]" value="portaluk"></center></td>
            <td><center><input type = "checkbox" name="portal[]" value="portalspain"/></center></td>
                <td><center><input type = "checkbox" name="portal[]" value="portalfrance"/></center></td>
                    <td><center><input type = "checkbox" name="portal[]" value="portalbenelux"/></center></td>
                        <td><center><input type = "checkbox" name="portal[]" value="portalgermany"/></center></td>
                            <td><center><input type = "checkbox" name="portal[]" value="portalsweeden"/></center></td>
    </tr><tr>
    <tr>
        <td>iRepair</td>
        <td><center><input type = "checkbox" name="irepair[]" value="irepairuk"/></center></td>
            <td><center><input type = "checkbox"name="irepair[]" value="irepairspain"/></center></td>
                <td><center><input type = "checkbox" name="irepair[]" value="irepairfrance"/></center></td>
                    <td><center><input type = "checkbox"name="irepair[]" value="irepairbenelux"/></center></td>
                        <td><center><input type = "checkbox"name="irepair[]" value="irepairgermany"/></center></td>
                            <td><center><input type = "checkbox"name="irepair[]" value="irepairsweeden"/></center></td>
    </tr>

</table>


        </applications>

    <servers>
    <!--<h2>Servers</h2><br><input type="checkbox" name="serv[]" value="phoenix">Phoenix<br><br>
    <input type="checkbox" name="serv[]" value="leasea">Lease A<br><br>

    </servers>
    <countries1><h2>Countries</h2><br><input type="checkbox" name="country" value="uk">UK<br><br>
    <input type="checkbox" name="country[]" value="germany">Germany<br><br>
    <input type="checkbox" name="country[]" value="france">France<br><br>

    </countries1>
    <countries2>
    <input type="checkbox" name="country[]" value="spain">Spain<br><br>
    <input type="checkbox" name="country[]" value="benelux">Benelux<br>
    <input type="checkbox" name="country[]" value="sweeden">Sweeden<br><br>
    </countries2> -->

    <submitb>

    <input type="submit" value="Update">

    </submitb>

    </form>


</body>
</html>

完成.php

<?php

 // First we execute our common code to connection to the database and start the session 
    require("common.php"); 


 //Check whether the form has been submitted
if (array_key_exists('check_submit', $_POST)) {
   //Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)
     $arr = $_POST["imanage"];
        try {

            // depend on your server setting, you might need this to put it on.
           //$glb_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

            //Loop through all row-value from the forms 
            foreach($arr as $key=>$value){

               if(count(array_filter($value)) >0){

               $data = array( 'UK' => $value['UK'], 'SPAIN' => $value['SPAIN'], 'FRANCE' => $value['FRANCE'], 'BENELUX' => $value['BENELUX'], 'GERMANY' => $value['GERMANY'], 'SWEEDEN' => $value['SWEEDEN'] );  
              $query = $glb_conn->prepare("INSERT INTO imanage (UK, SPAIN, FRANCE,BENELUX,GERMANY,SWEEDEN) VALUES (:UK, :SPAIN, :FRANCE, :BENELUX, :GERMANY, :SWEEDEN)");                           

               $query->bindValue(':UK', $value['UK']);
               $query->bindValue(':SPAIN', $value['SPAIN']);
               $query->bindValue(':FRANCE', $value['FRANCE']);  
               $query->bindValue(':BENELUX', $value['BENELUX']);                                      
               $query->bindValue(':GERMANY', $value['GERMANY']);                        
               $query->bindValue(':SWEEDEN', $value['SWEEDEN']);                        
               $query->execute($data); 
                }
            }

        } //end try
        catch(PDOException $e) {
            echo $e->getMessage();
        }


 /*  if ( isset($_POST['ireport']) ) { 
     $print = implode(', ', $_POST['ireport']); //Converts an array into a single string
   echo $print;
    echo('<br>');
   }
    if ( isset($_POST['iquote']) ) { 
     $print = implode(', ', $_POST['iquote']); //Converts an array into a single string
   echo $print;
   echo('<br>');
}

  if ( isset($_POST['portal']) ) { 
     $print = implode(', ', $_POST['portal']); //Converts an array into a single string
   echo $print;
   echo('<br>');
}
    if ( isset($_POST['irepair']) ) { 
     $print = implode(', ', $_POST['irepair']); //Converts an array into a single string
   echo $print;
   echo('<br>');
}*/

} else {
    echo "You can't see this page without submitting the form.";
}
?>

当我运行它时,我得到:未定义的索引:西班牙注意:未定义的索引:法国(对所有列出的国家都这样做)。我不确定如何解决这个问题。任何帮助将不胜感激。

编辑:一些研究建议使用 print_r 可以帮助我诊断问题。print_r($arr[0]) 给出。这让我更加困惑,因为在我看来它应该可以工作。数组([UK] => UK [SPAIN] => SPAIN [FRANCE] => FRANCE [BENELUX] => BENELUX [GERMANY] => GERMANY [SWEEDEN] => SWEEDEN)

4

1 回答 1

1

请尝试执行以下操作,希望您能看到错误所在。然后每个人都会很乐意提供帮助......

处理此问题的方法:

1.Recommended: Declare your variables. Or use isset() to check if they are declared before referencing them.
2.Set a custom error handler for E_NOTICE and redirect the messages away from the standard output (maybe to a log file). set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT).
3.Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is error_reporting( error_reporting() & ~E_NOTICE ).
4.Suppress the error with the @ operator.

注意:强烈建议只执行第 1 点。

来源:->这是来源

于 2013-10-29T13:50:00.243 回答