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?

Kazik Jarmolovski
  • 211
  • 1
  • 3
  • 9

3 Answers3

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.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

Use AND or OR in your WHERE statement

DELETE FROM wp_posts
WHERE post_parent = '$dbProductId' AND ID != '$dbProductThumbnail'
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
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'

Jwalin Shah
  • 2,451
  • 1
  • 17
  • 22