想要使用复选框和保存按钮更新多个记录

时间:2013-07-12 07:40:36

标签: php

我正在使用两个文本框搜索某些记录,然后更新数据库中的所选记录。我能够看到所选复选框的值行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());
}
}
?>

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分: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