简体   繁体   中英

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. The code within the form is <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.

How does one go about updating a mysql table with only one such code block? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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