Jump to content


Photo

DELETE with variable


  • Please log in to reply
5 replies to this topic

#1 ludwigvb

ludwigvb
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 30 May 2006 - 02:22 PM

Helloz,

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

$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

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

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 30 May 2006 - 03:05 PM

This is a bit odd. How can a row have cat_id=71 AND cat_id=72 AND... ?
~ D Kuang

#3 ludwigvb

ludwigvb
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 30 May 2006 - 03:21 PM

[!--quoteo(post=378388:date=May 30 2006, 06:05 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ May 30 2006, 06:05 PM) View Post[/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:

DELETE FROM categories WHERE cat_id=71 AND cat_id=72 AND cat_id=70

should it delete all the rows that have the specific cat_id s?

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 30 May 2006 - 03:23 PM

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
~ D Kuang

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 May 2006 - 03:25 PM

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] FROM categories WHERE cat_id=71 OR cat_id=72 OR cat_id=70 [!--sql2--][/div][!--sql3--]

Say each outloud and see which makes more sense.

Ken


#6 ludwigvb

ludwigvb
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 30 May 2006 - 03:29 PM

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 poirot




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users