简体   繁体   English

phpmyadmin mysql语法错误-多处条件

[英]phpmyadmin mysql syntax error - multi where conditions

I need help with my query syntax. 我的查询语法需要帮助。 I am using phpmyadmin 4 and mysql (not sure of version but suspect it is 5.6. My database has a table called places that contains columns id, city, state, sightings and date . My website takes input from the user via $_POST["city"] and $_POST["state"] . I want to increment sightings by 1 where the table entries for city and state match. 我使用的是phpmyadmin 4和mysql(不确定版本,但怀疑是5.6。我的数据库有一个名为places的表,其中包含列id, city, state, sightings and date 。我的网站通过$_POST["city"]接收用户的输入。 $_POST["city"]$_POST["state"] 。我想使城市和州的表项匹配的瞄准点增加1。

Using phpmyadmin's SQL query builder, I tested possible query structures using place names rather than variables. 使用phpmyadmin的SQL查询生成器,我使用地名而不是变量测试了可能的查询结构。 The following query was successful: 以下查询成功:

UPDATE places SET sightings = sightings +1 WHERE city =  "Denver" AND state =  "Colorado"

I then clicked "create PHP code" which produced the following: 然后,我单击“创建PHP代码”,产生了以下内容:

"UPDATE places SET sightings=sightings+1 WHERE city = \"Denver\" AND state = \"Colorado\"";

I inserted this code, substituting the place names for variables declared from $_POST["city"] and $_POST["state"] ... 我插入了这段代码,用地名替换了从$_POST["city"]$_POST["state"]声明的变量...

$result = cs50::query("UPDATE places SET sightings=sightings+1 WHERE city = \"{$city}\" AND state = \"{$state}\"");

and got error message: 并收到错误消息:

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 ''Denver', 'Colorado')'

I have tinkered with this line of code for hours and can't get it to work. 我已经修改了这行代码数小时,无法正常工作。 The fact it works in phpmyadmin means its doable, I am just coding it wrong. 它在phpmyadmin中工作的事实意味着它是可行的,我只是将其编码为错误。

The function query is defined in a custom library provided by the course I am studying, CS50. 函数查询是在我正在研究的课程CS50提供的自定义库中定义的。 I wonder whether there is some limitation to the function (can post the code if interested) or am I just using the wrong syntax??? 我想知道功能是否有一些限制(如果感兴趣的话可以发布代码)还是我使用的语法错误???

Any help is welcome, apologies if my question is confusing, this is relatively new to me. 欢迎任何帮助,如果我的问题令人困惑,我们深表歉意,这对我来说是相对较新的。

Have you tried: 你有没有尝试过:

"UPDATE places SET sightings=sightings+1 WHERE city = 'Denver' AND state = 'Colorado'";

I've always found that single quotes are more correct than double quotes, even if they are escaped. 我一直发现单引号比双引号更正确,即使它们被转义了。

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

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