i have a page that has a product, and this product has one to many sizes that the user can add or remove by using checkboxes.
i have checkboxes that are appearing correctly and are either checked or unchecked, i am having troubles getting the changes to these checkboxes updated into the database,
here is my checkbox code so you can see the name..
<input name="size_id[]" type="checkbox" id="size_id[]" value="">
here is my update code..
if (isset($_POST['size_id'])){
foreach ($_POST['size_id'] as $size_id) {
echo "checkbox value checked: " .$size_id ."<BR>";
//Delete from sizesball (the join table) only those that have been deselected
if ($size_id <> "") {
$updateSQL2 = sprintf("DELETE FROM balls_sizes WHERE size_id not in(%s) and ball_id=%s",
$size_id,
GetSQLValueString($_POST['ball_id'], "int"));
mysql_select_db($database_cnnTest, $cnnTest);
$Result2 = mysql_query($updateSQL2, $cnnTest) or die(mysql_error());
}
//Now insert any newly selected sizes into sizesball
}
foreach ($_POST['size_id'] as $size_id) {
//First check to see if that size is already in the table sizesball
mysql_select_db($database_cnnTest, $cnnTest);
$query_rssizesballs = sprintf("SELECT size_id, ball_id FROM balls_sizes where ball_id =%s and size_id = %s",
GetSQLValueString($_POST['ball_id'], "int"), $size_id);
$rssizesballs = mysql_query($query_rssizesballs, $cnnTest) or die(mysql_error());
$row_rssizesballs = mysql_fetch_assoc($rssizesballs);
$totalRows_rssizesballs = mysql_num_rows($rssizesballs);
//If the recordset is empty, we can insert a new record
if($totalRows_rssizesballs == 0) {
$updateSQL3 = sprintf("INSERT INTO balls_sizes (ball_id,size_id) VALUES (%s,%s)",
GetSQLValueString($_POST['ball_id'], "int"), $size_id);
$Result3 = mysql_query($updateSQL3, $cnnTest) or die(mysql_error());
}
}
}