Kazik Jarmolovski
Kazik Jarmolovski

Reputation: 211

Deleting rows from mysql database

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?

Upvotes: 1

Views: 180

Answers (3)

Jwalin Shah
Jwalin Shah

Reputation: 2521

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'

Upvotes: 0

Sumit Bijvani
Sumit Bijvani

Reputation: 8179

Use AND or OR in your WHERE statement

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

Upvotes: 0

John Woo
John Woo

Reputation: 263683

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.

Upvotes: 3

Related Questions