Jump to content

DELETE with variable


ludwigvb

Recommended Posts

Helloz,

I'm trying to make a command to delete some mysql rows in a script.
The code I use is:

[code]
$subcats = catDelete($cat_id); // gets the subcategories of the top category

echo $subcats; //  just a test to see that it outputs the right code
echo '<br>'. $cat_id; // just a test to see the the $cat_id variable has not been changed

    $db = mysql_connect($server_name,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    mysql_query("DELETE FROM categories WHERE $subcats cat_id = $cat_id",$db) or die(mysql_error());  

echo $lang_info_del; // echos that the categories and subcategories has been deleted
[/code]

echo commands are to make sure I get the right code for the mysql query that deletes the rows
$subcats echos:
cat_id=71 AND cat_id=72 AND

When I try to delete a category that has sub categories rows are not getting deleted
When I try to delete a category with no subcategories the command is executed and the row gets deleted.

Any tips?
Thanks in advance!

Spyros
Link to comment
Share on other sites

[!--quoteo(post=378388:date=May 30 2006, 06:05 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ May 30 2006, 06:05 PM) [snapback]378388[/snapback][/div][div class=\'quotemain\'][!--quotec--]
This is a bit odd. How can a row have cat_id=71 AND cat_id=72 AND... ?
[/quote]

The table "categories" I have is like this:

cat_id | cat_name
70 | name01
71 | name02
72 | name03


If I executre the query:

[code]DELETE FROM categories WHERE cat_id=71 AND cat_id=72 AND cat_id=70[/code]

should it delete all the rows that have the specific cat_id s?
Link to comment
Share on other sites

No, it won't delete a thing, because that would mean:
cat_id is 71, and is 72, and 70. This doesn't make any sense, it's one or another.

If you want to delete all rows with any of these id's, use cat_id=x OR cat_id=y
Link to comment
Share on other sites

No. You need to use the "OR" boolean.
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']DELETE[/span] [color=green]FROM[/color] [color=orange]categories[/color] [color=green]WHERE[/color] cat_id[color=orange]=[/color]71 [color=blue]OR[/color] cat_id[color=orange]=[/color]72 [color=blue]OR[/color] cat_id[color=orange]=[/color]70 [!--sql2--][/div][!--sql3--]

Say each outloud and see which makes more sense.

Ken
Link to comment
Share on other sites

I tried this just before your post... it worked (ofcourse)

I spent 2 hours tryign to figure out what was wrong.

Another question is:

Am I stupid?

BUT I have to answear this by my own !!! [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

Thanks for your help [b]poirot[/b]
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.