View Full Version : update db on checkboxes

11-12-2011, 09:16 PM
Now that I've got my form populating, I'm trying to get it to update the database.

It's a series of checkboxes with the name 'option2' but different values. For those that are checked, I want to add an entry to the database. For those that are unchecked I want to delete an entry from the database if it exists.

Here's what I'm trying right now, but I must have a syntax error? I'm getting "Invalid argument supplied for foreach() "

$id = 24;
foreach($_POST['option2'] as $row) {
$list2 = implode(',', $row);
$sql2a = "DELETE FROM plant_edible_link WHERE plant_id = $id and edible_id NOT IN ($list2)";
$sql2b = "INSERT INTO plant_edible_link(plant_id, edible_id) VALUES ($list2)";

11-12-2011, 11:39 PM
a little progress in understanding implode/explode

I can see my results. but now I'm trying to figure out how to turn it into an insert or delete query action...

$imploder=implode(',', $_POST['option2']);
//echo $imploder."<br>";
$exploder = explode(',', $imploder);


//$add = ???;
//$delete = ????;


11-13-2011, 04:10 AM
Post the output of $_POST['option2'] so we know what data we're dealing with: