简体   繁体   English

如何使用复选框的单列从HTML表单更新MYSQL表

[英]How to update MYSQL table from HTML form with single column of checkboxes

I have a MYSQL table named 'orders' that I want to update from a single column of checkboxes in an html form. 我有一个名为'orders'的MYSQL表,我想从HTML形式的复选框的单个列中进行更新。 The code within the form is <input type='checkbox' name='completed[]' value='';> 表单中的代码为<input type='checkbox' name='completed[]' value='';>

I've looked around for a long time to see how I could submit this form and update my database with this single line of code. 我环顾了很长时间,看看如何提交此表单并使用这行代码更新数据库。 In other words, the column of checkboxes consists only of this code block, but there is a checkbox at the end of each row. 换句话说,复选框列仅由该代码块组成,但是每行的末尾都有一个复选框。 I know that only the checked boxes will be returned in the $_POST['completed'] array. 我知道$_POST['completed']数组中只会返回选中的框。

How does one go about updating a mysql table with only one such code block? 如何只用一个这样的代码块更新mysql表? The update code is this: 更新代码是这样的:

update = "UPDATE orders SET completed='$completed' WHERE completed=0;"; 

Then 然后

$res = mysqli_query($db, $sql) or die(mysql_error()); //update or error

Probable answer: 可能的答案:

$completed = implode(",", $completed);
$sql = "UPDATE orders SET completed='1' WHERE completed=0 AND orders.id IN ($completed)"; 

assuming your $_POST['completed'] has ids of orders which are completed and completed column has only boolean values. 假设您的$ _POST ['completed']具有已完成订单的ID,并且completed列仅具有布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM