0

我正在使用两个文本框搜索一些记录,然后更新数据库中的选定记录。我能够看到所选复选框的值行 ID,但是当我想在数据库中获取更新值时它给出 0,即在数组中没有显示记录

这是我的代码

if($_POST["search"])
{
    $nitnumber = $_POST["nitnumber"];
    $workno = $_POST["workno"];
    $query = "select * from print where nit = $nitnumber and work = $work";
    $result = mysql_query($query) or die ("<font color =red>NIT Number and/or Work Number is Missing</font>");
    $count = mysql_num_rows($result);
if($count == 0)
    echo "<font color=red>Record not found</font>";
                else
                {
                while($record = mysql_fetch_assoc($result))
                {
                ?>  
  <tr class="odd">
  <td><div align="center"><?php echo $record['a']; ?></div></td>
  <td><div align="center"><?php echo $record['b']; ?></div></td>
  <td><div align="left"><?php echo $record['c']; ?></div></td>
  <td> <div align="left">
<?php 
   enter code hereecho $record["d"];    
           ?>                   
          </td>
            <td> 
                <input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>">                        
                <?php echo $record["id"];?>
            </td>
            <?php } } }?>
            <tr>
                <td colspan="5" align="right"> <input type="submit" value="Save" name="save"> </td>
            </tr>

<?php    
 if ($_POST['save'])
{
       $num_chkboxes=count($_POST['checkbox']);
       for($i=0; $i<$num_chkboxes; $i++){
       $complete = intval($checkbox[$i]);
       echo $complete;
       var_dump($complete);
       echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
       //$Result1 = mysql_query($updateSQL, $FamilyOrganizer) or die(mysql_error());
}
}
?>
4

2 回答 2

0
    <?php    
     if ($_POST['save'])
    {

       $checkbox1 = $_POST['chk1'];
        $selected_checkbox = "";
        foreach ($checkbox1 as $checkbox1) 
        {
            $selected_checkbox .= $checkbox1 . ", ";
        }
        $selected_checkbox = substr($selected_checkbox, 0, -2);
       $updateSQL = "" // your update query here
    }
?>    
    <input type="checkbox" name="chk1[]" value=""> // take checkboxes like this
于 2013-07-12T07:59:06.507 回答
0

首先,当您填写复选框的值时,问题就开始了:

<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>" />

你必须给他们像

value="<?php echo $record['id'] ?>"

其次,您没有得到应有的复选框的值:

for($i = 0; $i < count($_POST['checkbox']); $i++){
   $complete = intval($_POST['checkbox'][$i]);
   //echo $complete;
   //var_dump($complete);
   echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
}

您应该注意的另一件事是元素 id 的分配:

id="checkbox[]"

您必须为每个元素提供唯一的 ID:

id="checkbox-1"
id="checkbox-2"
于 2013-07-12T07:47:05.573 回答