vozzek Posted November 28, 2007 Share Posted November 28, 2007 Hi all, I've got some php code running an SQL delete to remove rows from my shopping cart table (tbl_cart). It works on individual cart id's, but not when I want the entire cart emptied. tbl_cart looks like this: ct_id (cart id, this is the primary key) pd_id (product id) ct_qty (quantity) ct_session_id (user's unique session id) etc... Here's the code: <?php // current session id $sid = session_id(); // $cid passed from view_cart page // if $cid=0, then empty cart if ($cid != 0) { $sql = "DELETE FROM tbl_cart WHERE ct_id = $cid"; // This works } else { $sql = "DELETE FROM tbl_cart WHERE ct_session_id = $sid"; // This doesn't work } $result = mysql_query($sql) or die(mysql_error()); ?> If I pass $cid as a whole number, it deletes that row - effectively deleting that item from the cart. But when the user presses the EMPTY CART button, I call the above code by passing $cid as zero. At this point I want all records deleted that match the current user's session id, which is $sid. The error I'm getting is: Unknown column '785fdf3a1a755e2a2189fa3028f33f22' in 'where clause' The crazy variable is of course, the session id. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/79320-solved-deleting-multiple-rows/ Share on other sites More sharing options...
fenway Posted November 28, 2007 Share Posted November 28, 2007 Echo $sql. Link to comment https://forums.phpfreaks.com/topic/79320-solved-deleting-multiple-rows/#findComment-401481 Share on other sites More sharing options...
vozzek Posted November 28, 2007 Author Share Posted November 28, 2007 I added an echo just before the mysql_query($sql). Here's what printed: DELETE FROM tbl_cart WHERE ct_session_id = 21ea32ef26924cf9e962a2b4f25f34bbUnknown column '21ea32ef26924cf9e962a2b4f25f34bb' in 'where clause' Link to comment https://forums.phpfreaks.com/topic/79320-solved-deleting-multiple-rows/#findComment-401485 Share on other sites More sharing options...
toplay Posted November 29, 2007 Share Posted November 29, 2007 Use quotes around any values (especially strings). So, change this: $sql = "DELETE FROM tbl_cart WHERE ct_session_id = $sid"; To this: $sql = "DELETE FROM tbl_cart WHERE ct_session_id = '$sid'"; Link to comment https://forums.phpfreaks.com/topic/79320-solved-deleting-multiple-rows/#findComment-401800 Share on other sites More sharing options...
vozzek Posted November 29, 2007 Author Share Posted November 29, 2007 Pefect! Thanks so much, I should've thought of that. Link to comment https://forums.phpfreaks.com/topic/79320-solved-deleting-multiple-rows/#findComment-402232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.