1

I have a problem deleting rows from table with parameters. Here is the code:

if ($dbProductImageIdNumber) { $ImagesToDeleteNumber = $dbProductImageIdNumber -1 - $numItemImages; echo "$dbProductImageIdNumber" . ' '; echo "$numItemImages" . ' '; echo "$ImagesToDeleteNumber" . ' '; echo 'Deleting'; mysql_query("DELETE FROM wp_posts WHERE post_parent = '$dbProductId' ,ID != '$dbProductThumbnail'"); } 

Problem is that != seems to be understood wrongly, maybe I'm dooing syntax mistakes?

Will apreciate any help.

-----EDIT-----

Ok, here what I have now:

if ($dbProductImageIdNumber) { $ImagesToDeleteNumber = $dbProductImageIdNumber -1 - $numItemImages; echo "$dbProductImageIdNumber" . ' '; echo "$numItemImages" . ' '; echo "$ImagesToDeleteNumber" . ' '; echo "$dbProductId" . ' '; echo "$dbProductThumbnail" . ' '; mysql_query("DELETE FROM wp_posts WHERE post_parent = '$dbProductId' AND ID != '$dbProductThumbnail'"); } 

Example: my $dbProductId is '16' and $dbProductThumbnail '17'. Qestion is, why this command do not delete any row where post_parent is '16' and ID isn't 17? Any leads?

3 Answers 3

3

it should be AND and not comma.

DELETE FROM wp_posts WHERE post_parent = '$dbProductId' AND ID != '$dbProductThumbnail' 

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Sign up to request clarification or add additional context in comments.

Comments

0

Problem is not != but the problem was that you have used "," instead of AND in your code between post_parent = '$dbProductId' ,ID != '$dbProductThumbnail'"

mysql_query("DELETE FROM wp_posts WHERE post_parent = '$dbProductId' ,ID != '$dbProductThumbnail'"); }

So, corrent syntax is below.

DELETE FROM wp_posts WHERE post_parent = '$dbProductId' AND ID != '$dbProductThumbnail'

4 Comments

Updated my post, please look there. AND was a mistake, but it didn't help.
DELETE FROM wp_posts WHERE post_parent = '$dbProductId' AND ID <> '$dbProductThumbnail'
With <> it deletes everything even a row where ID is $dbProductThumbnail.
Oh, that was my mistake.. $dbProductThumbnail was empty. So now it works! :) Thank you very much! It seems all these answers were rigth then, but I suppose I will mark yours as you helped me the most, and as I can see you need rep points the most too. :)
0

Use AND or OR in your WHERE statement

DELETE FROM wp_posts WHERE post_parent = '$dbProductId' AND ID != '$dbProductThumbnail' 

1 Comment

Updated my post, please look there. AND was a mistake, but it didn't help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.