dqqyp90576 2014-01-28 12:15
浏览 38
已采纳

插入所选复选框的隐藏字段

i am having a table with check boxes i want to insert specific hidden values of a row in database which is selected by the user using a checbox. But when ever i used to save the record in database. It insert only one record that is even not selected and is the top one in the table on the site. Here is my code

<?php 
    $i = 0;                         
    <td><?php echo $record['a'];?></td>
    <td><?php echo $record['name']; ?>
    <input type="hidden" name="name[]" value="<?php echo $record['name']; ?>"</td>
    <td><?php echo $record['contact'];?>
        <input type="hidden" name="contact[]" value="<?php echo $record['contact']; ?>"
    </td>
    <td><?php echo $record['district'];?>
        <input type="hidden" name="district[]" value="<?php echo $record['district']; ?>"
    </td>
    <td>
        <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $i++;?>" />
    </td>
<?php   }?>
    <input type="submit" value="Save" name="save" />
<?php     
    if ($_POST['save'])
    {
        foreach ($_POST['checkbox'] as $i)
        {   
            $save = "insert into tablename(id,name,contact,district) values (NULL, '{$_POST['name']'[$i]}'{$_POST['contractorname'][$i]}','{$_POST['district'][$i]}')";
            mysql_query($save) or die("Mistake in Query");
        }  
    echo "Record saved Successfully";
    }

?>    
  • 写回答

3条回答 默认 最新

  • duanlan3598 2014-01-28 12:44
    关注

    You set $i = 0; inside that loop So each time the loop is run through, $i = 0; resets your counter to 0. This results in all your checkboxes having the same value 0. To resolve this, set $i = 0; before the loop starts

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)