doulu3399 2018-12-04 20:01
浏览 94
已采纳

php - 使用复选框在循环内更新mysql

I'm trying to update all selected fields in the database using check box. This is the code that I've done, but it only updates the last record I choose from the checkbox list.

<tbody>
<?php while($row = mysqli_fetch_array($queryEmail)):?>
<tr>    
<td><input type="checkbox" name="formDoor[]" value=<?php echo $row['id'];?>> 
</td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['hour'];?></td>             
</tr>
<?php endwhile;?>
</tbody>
<?php
$hor = $_POST['hours']; //input value i want to update in selected id's
foreach ($_POST['formDoor'] as $entry){  
$sql = "UPDATE hora SET hour='$hor' WHERE id = $entry";  
}
?>
  • 写回答

1条回答 默认 最新

  • douyun8885 2018-12-04 20:18
    关注

    Try this You are forget to execute mysqli_query() on loop

    <tbody>
    <?php while($row = mysqli_fetch_array($queryEmail)):?>
        <tr>    
            <td>
                <input type="checkbox" name="formDoor[]" value=<?php echo $row['id'];?>> 
            </td>
            <td><?php echo $row['name'];?></td>
            <td><?php echo $row['hour'];?></td>             
        </tr>
    <?php endwhile;?>
    </tbody>
    
    <?php
    $hor = $_POST['hours']; //input value i want to update in selected id's
    foreach ($_POST['formDoor'] as $entry){  
       $sql = mysqli_query($con,"UPDATE hora SET hour='$hor' WHERE id = $entry");     
    }    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络