简体   繁体   English

从mysql数据库中删除行

[英]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'. 示例:my $ dbProductId为'16',$ dbProductThumbnail为'17'。 Qestion is, why this command do not delete any row where post_parent is '16' and ID isn't 17? 问题是,为什么这个命令不删除post_parent为'16'且ID不是17的任何行? Any leads? 任何线索?

it should be AND and not comma. 它应该是AND而不是逗号。

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. 作为一个旁注,查询是脆弱的SQL Injection ,如果变量的值(一个或多个 )从外面走了进来。 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. 通过使用PreparedStatements您可以摆脱使用值周围的单引号。

Use AND or OR in your WHERE statement WHERE语句中使用ANDOR

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

Problem is not != but the problem was that you have used "," instead of AND in your code between post_parent = '$dbProductId' ,ID != '$dbProductThumbnail'" 问题不是!=但问题是你在post_parent ='$ dbProductId',ID!='$ dbProductThumbnail'之间的代码中使用了“,”而不是AND

mysql_query("DELETE FROM wp_posts WHERE 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' DELETE FROM wp_posts WHERE post_parent ='$ dbProductId'AND ID!='$ dbProductThumbnail'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM