简体   繁体   中英

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.

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.

Perhaps you want an UPDATE statement instead?

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. Are you trying to update existing database records instead? In that case, use the Update statement.

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

where clause can not be used in INSERT statment

please read this before preceding further http://dev.mysql.com/doc/refman/5.5/en/insert.html

您要做的是:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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