简体   繁体   English

如何在用PHP编写的SQL查询中正确指定WHERE子句?

[英]How do I correctly specify the WHERE clause in this SQL query written in PHP?

$query = "INSERT INTO directory_level_one (child_categories)
    VALUES 
        ('$category_name')
    WHERE
        category = '$parent'";

currently, I get the following error when I add the WHERE part in the above sql query. 当前,在上述sql查询中添加WHERE部分时,出现以下错误。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE category = 'Philosophy'' at line 4

INSERT statements don't have a WHERE clause. INSERT语句没有 WHERE子句。

Perhaps you want an UPDATE statement instead? 也许您想要使用UPDATE语句?

UPDATE directory_level_one
SET child_categories = 'your_category_name'
WHERE category = 'your_parent'

You can't have a where clause for an Insert statement. 您不能在Insert语句中使用where子句。 Are you trying to update existing database records instead? 您是否要更新现有的数据库记录? In that case, use the Update statement. 在这种情况下,请使用Update语句。

您不能在插入语句中使用where子句。

where clause can not be used in INSERT statment 在INSERT语句中不能使用where子句

please read this before preceding further http://dev.mysql.com/doc/refman/5.5/en/insert.html 请先阅读此内容,然后再继续http://dev.mysql.com/doc/refman/5.5/zh-CN/insert.html

您要做的是:

$query = "UPDATE directory_level_one SET child_categories='$category_name' WHERE category = '$parent'";

我认为您可能想将INSERT更改为UPDATE

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

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