svgmx5 Posted January 24, 2011 Share Posted January 24, 2011 Hey everyone i having some trouble updating records in my databse using checkbox from a form. I can update the records, but i can't figure out how to remove records at the same time. I've posted the code that i'm using to update below foreach($_POST['tag'] as $k=>$l){ $tag = mysql_real_escape_string($_POST['tag'][$k]); $check_tags = mysql_query("SELECT * FROM file_cats WHERE category='$tag'") or die(mysql_error()); $num_tags = mysql_num_rows($check_tags); if($num_tags==0){ $add_tag = mysql_query("INSERT into file_cats (fileID, category) VALUES('$fileID', '$tag')") or die(mysql_error()); } } This code inserts new records based on what the user checked. If it exist it won't add a new row, if it doesn't exist then it will add a new row. but like i said how can remove a row if the box is no longer checked? I've tried using foreach(empty($_POST['tag'] as $k => $l){ $tag = empty($_POST['tag']['$k']; } But that didn't work basically nothing was deleted. Has anyone done something like this before? if so i would appreciate any help you could give Quote Link to comment https://forums.phpfreaks.com/topic/225567-add-delete-rows-in-database-using-checkboxes/ Share on other sites More sharing options...
DavidAM Posted January 25, 2011 Share Posted January 25, 2011 Yeah, HTTP does not post checkboxes that are not checked. I guess the easiest thing to do is to delete the entries that are not in your posted array: $allTags = array(); foreach($_POST['tag'] as $k=>$l){ $tag = mysql_real_escape_string($_POST['tag'][$k]); // Collect all the tags - escaped and quoted - in an array // this just simplifies the implode() later $allTags[] = "'$tag'"; $check_tags = mysql_query("SELECT * FROM file_cats WHERE category='$tag'") or die(mysql_error()); $num_tags = mysql_num_rows($check_tags); if($num_tags==0){ $add_tag = mysql_query("INSERT into file_cats (fileID, category) VALUES('$fileID', '$tag')") or die(mysql_error()); } } // Delete all tags EXCEPT the ones we just added if (! empty($allTags)) { $sql = "DELETE FROM file_cats WHERE fileID = $fileID AND category NOT IN (" . implode(','$allTags) . ")"; mysql_query($sql); } NOTE: Your SELECT query seems to be missing AND fileID = $fileID - maybe you were just typing fast for the post, or maybe your code is not checking what you think it is checking. Quote Link to comment https://forums.phpfreaks.com/topic/225567-add-delete-rows-in-database-using-checkboxes/#findComment-1164787 Share on other sites More sharing options...
svgmx5 Posted January 25, 2011 Author Share Posted January 25, 2011 I guess that explains why nothing was removed. yea i needed that fileid='$fileID' i just forgot to type it in Anyway i'll try it out, and i think i can take it from here...thanks again, i'll post if i have any problems or if it works Quote Link to comment https://forums.phpfreaks.com/topic/225567-add-delete-rows-in-database-using-checkboxes/#findComment-1164816 Share on other sites More sharing options...
svgmx5 Posted January 25, 2011 Author Share Posted January 25, 2011 So now i'm getting the following error: Parse error: syntax error, unexpected T_VARIABLE in on line 67 Line 67 is this: $remove_tags = mysql_query("DELETE FROM file_cats WHERE fileID='$fileID' AND category NOT IN(". implode(',' $allTags) .")") or die(mysql_error()); I've looked in or around line 67 to see if i forgot to close a tag or semicolon, but i'm pretty sure it has to do with the AND CATEGORY NOT IN (".implode(',' $allTags).") Because i removed it and the page displayed.. Quote Link to comment https://forums.phpfreaks.com/topic/225567-add-delete-rows-in-database-using-checkboxes/#findComment-1164832 Share on other sites More sharing options...
svgmx5 Posted January 25, 2011 Author Share Posted January 25, 2011 Okay so i got that error fixed...the coma was in the wrong place... However when deleting its looking for a column named after the category rather than just looking in the category column this is the error i get.... You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tag1,tag2,tag3)' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/225567-add-delete-rows-in-database-using-checkboxes/#findComment-1164834 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2011 Share Posted January 25, 2011 When you implode() an array in an IN() like that, don't forget you have to account for quoting the values in it. $query = "DELETE FROM file_cats WHERE fileID='$fileID' AND category NOT IN('" . implode("', '", $allTags) . "')"; $remove_tags = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/225567-add-delete-rows-in-database-using-checkboxes/#findComment-1164855 Share on other sites More sharing options...
svgmx5 Posted January 25, 2011 Author Share Posted January 25, 2011 thanks, that fixed that issue! looks like i got it fixed now thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/225567-add-delete-rows-in-database-using-checkboxes/#findComment-1165057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.