PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
This loop currently adds products to a table using $key.
I want to delete records from this table where the id ($key) does not occur in the loop.
So say the loop contained keys 1,2,3 i would want the query to delete all references from the table where the id's were not 1, 2 or 3. Any ideas on how to do this?
I've posted this in the code and not query section as it's the code to find the id's not in the loop i need.
$ids = array();
while(list($key) = each($_POST["add"]))
{
$ids[] = $key;
//update/insert queries will go here
}
$clear = "DELETE FROM form_products WHERE form_product NOT IN('.implode(',', $ids).') AND form_cat='$_POST[cat]' AND form_id='$_POST[id]'";
$cq = mysql_query($clear) or die ("Query $clear Failed".mysql_error());
The final query seems to delete everything, not just the ones in $ids, any idea what i'm doing wrong?
Last edited by rsmarsha on Fri Aug 11, 2006 3:16 am, edited 1 time in total.
$sql = "DELETE FROM `form_products` WHERE `form_product` NOT IN(".implode(",", $ids).") AND `form_cat` = '".$_POST['cat']."' AND `form_id` = '".$_POST['id']."'";
Query DELETE FROM `form_products` WHERE `form_product` NOT IN() AND `form_cat` = '' AND `form_id` = '44' FailedYou have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND `form_cat` = '' AND `form_id` = '44'' at line 1