richclever Posted July 26, 2007 Share Posted July 26, 2007 Hi all, I have been trying to figure this one out and have come up against a brick wall although I know it should be relatively simple. I have a form that passes an array to a php script from a multiple select box that is created dynamically. If a user deselects an item then I want to delete the record from the database but am having a big problem with it. The table in the dbase is quite simple with just three fields: id classid formid Basically, the form send through a classid and a formid value and I want to delete any records that have a classid that are not in the form array and have a formid which is passed as a hidden value. I suppose really what I want is to know how to see if a classid is not in the array that is passed. Hope that makes sense. Sorry I don't have any code but I can't get it even nearly right so what I have been playing with wouldn't be any use. Thanks, Richard Link to comment https://forums.phpfreaks.com/topic/61849-checking-if-a-value-is-not-in-an-array-and-deleting-record-from-database/ Share on other sites More sharing options...
Fadion Posted July 26, 2007 Share Posted July 26, 2007 I hope i got the concept right. Also i didnt try the code because i didnt have the database but i hope it gives the idea $values = $_POST['select']; //get the array from the multiple select $results = @mysql_query("SELECT * FROM table") or die(); while($rows = mysql_fetch_array($results){ for($i=0; $i<count($values); $i++){ //a loop to get all the array values if($rows['id'] == $values[$i]){ //if the curret id is finded in the array then exit and do nothing $deleteRow = false; exit; } else{ //if it is not finded then it will delete $deleteRow = true; } } if($deleteRow == true){ $queryDelete = "DELETE FROM table WHERE id='{$rows['id']}'"; } } Link to comment https://forums.phpfreaks.com/topic/61849-checking-if-a-value-is-not-in-an-array-and-deleting-record-from-database/#findComment-308156 Share on other sites More sharing options...
yarnold Posted July 26, 2007 Share Posted July 26, 2007 Couldn't you just use in_array()? Link to comment https://forums.phpfreaks.com/topic/61849-checking-if-a-value-is-not-in-an-array-and-deleting-record-from-database/#findComment-308159 Share on other sites More sharing options...
Barand Posted July 26, 2007 Share Posted July 26, 2007 Rather than the "Hands up anyone who's not here" approach, it's easier if the user selects items to be deleted. Link to comment https://forums.phpfreaks.com/topic/61849-checking-if-a-value-is-not-in-an-array-and-deleting-record-from-database/#findComment-308199 Share on other sites More sharing options...
Fadion Posted July 26, 2007 Share Posted July 26, 2007 Couldn't you just use in_array()? Didnt know that. It shortens the code a lot Link to comment https://forums.phpfreaks.com/topic/61849-checking-if-a-value-is-not-in-an-array-and-deleting-record-from-database/#findComment-308247 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.