Jump to content

Help with deleting rows in database


svgmx5

Recommended Posts

I'm trying to delete rows in the database, based on what checkboxes are left empty by the user...

 

What i have is small application in which a user can add an item to the database and give it tags...

 

for example there is "apple" and it has the tags of "fruit, red, round"

 

What i want is for the user to be able to edit those tags, so the user can remove "fruit" and add "sweet" instead or just remove "fruit" all at the same time.

 

The code i have, somewhat works...

 

This code below loops through the checkboxes and should remove the items that are no longer checked, by going through the database and grabbing all the tags and them checking it against those that are still checked and remove the ones that it didn't find checked...??

 

However whats happening is when i unchecked say "red" it removes all the other items..so if i had "red, fruit, round' and i want to remove "red" it leaves red but removes the rest.

foreach($_POST['tag'] as $r=>$n){
			$t = mysql_real_escape_string($_POST['tag'][$r]);

			$get_tag2 = mysql_query("SELECT * FROM file_cats WHERE fileID='$fileID'") or die(mysql_error());
			while($t2 = mysql_fetch_assoc($get_tag2)){
				$tmp_tag1 = $t2['category'];		

				if($t==$tmp_tag1){
					$remove_tag = mysql_query("DELETE FROM file_cats WHERE fileID='$fileID' AND category='$t' LIMIT 1") or die(mysql_error());	
				}
			}
		}

 

This following piece of code just adds a new tag...however i want it to check to see if that tags already exist in the database, if it does then don't add it just add the new ones..

 

foreach($_POST['tag'] as $k=>$l){
			$tag = mysql_real_escape_string($_POST['tag'][$k]);

			$add_tag = mysql_query("INSERT into file_cats (fileID, category) VALUES('$fileID', '$tag')") or die(mysql_error());	

		}

 

My final problem is that i can't remove items if the second snippet of code is there, i have to remove those lines of code in order to remove items, however i can add items with both loops there.

 

I'm basically stuck and hope that someone here can shed some light on what i'm doing wrong, because i know i'm doing something wrong i just can't find it

 

Anyway thank you in advanced

 

 

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.