Reputation: 1
Mission: Verify that "players" have placed vintage promotional jpegs on their social media site(s). Doing so earns them crypto tokens towards game play. All non-verified postings are displayed on an administrator's dashboard with a checkbox if the listing has been visually verified and "Approved." The value of each checkbox is the database ID of the listing (see screen capture). Once the listings have been verified and the check box ticked, the page is POST(ed) to a query page where the default value "confirm" is changed from 0 to 1. This then awards them the designated tokens.
However, I haven't found the right formula of code to get the UDPATE to work. The checkbox values will display in "echo" mode, but the SQL won't function within the FOREACH loop.
<?php
$checked_arr = $_POST['checkbox'];
$count = count($checked_arr);
echo "There are ".$count." checkboxe(s) are checked <br>";
foreach($_POST['checkbox'] as $val) {
$sql = "UPDATE media SET confirm = 1 WHERE id = $val " ;
echo "$val\n";
?>
Scoured the Net for working examples, including Stack Overflow, but to no avail. Assume there's a simple answer here, but as a novice PHP programmer, it eludes me.
Thank you in advance for suggestions.
Upvotes: 0
Views: 206
Reputation: 58
you could use the IN operation in the conditional.
The only thing you would have to do is capture all the IDS and do an "implode" to give it the format that is needed for the sql.
for example:
<?php
$ids = [];
foreach($_POST['checkbox'] as $val){
$ids[] = $val;
}
$values = implode(",", $ids); // => 'id1, id2, id3, ...'
// this is a massive update based on selected items
$sql = "UPDATE media SET confirm = 1 WHERE id IN ($values)";
?>
Upvotes: 1