Jump to content

Checking if a value is not in an array and deleting record from database


richclever

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.