简体   繁体   English

我的更新查询出了什么问题?

[英]What's wrong with my update query?

Can you tell me what's wrong with this update statement? 你能告诉我这个更新声明有什么问题吗? Not updating my db record in mysql 不在mysql中更新我的db记录

$updateid = $row[id]; 

$result2 = mysql_query("UPDATE grades SET processed = 1
where 'id' = '$updateid'") or die(mysql_error()); 

ColumnNames ( as well as TableName ) shouldn't be enclosed with single quotes because they are identifiers and not string literals. ColumnNames以及TableName )不应该用单引号括起来,因为它们是标识符而不是字符串文字。 Wrapping an identifier with single quotes makes it string literals. 用单引号包装标识符使其成为字符串文字。

UPDATE grades 
SET processed = 1
where id = '$updateid'

If you are unsure if the columnName ( or TableName ) you are using is a reserved keyword, delimit it with backticks and not with single quotes. 如果您不确定您使用的columnName( 或TableName )是否为保留关键字,请使用反引号分隔,而不是使用单引号。 eg, 例如,

UPDATE `grades` 
SET `processed` = 1
where `id` = '$updateid'

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您可以摆脱使用值周围的单引号。

You are quoting your column name. 您正在引用您的列名称。 If you want to do that (it's not necessary here), you should use backticks: 如果你想这样做(这里没有必要),你应该使用反引号:

$result2 = mysql_query("UPDATE grades SET processed = 1
             where `id` = '$updateid'") or die(mysql_error());

Apart from that you should make sure that your variable is safe to use in an sql query, preferably using PDO (or mysqli) and prepared statements. 除此之外,您应确保您的变量在sql查询中使用是安全的,最好使用PDO(或mysqli)和预处理语句。 If you really want to use the deprecated mysql_* functions, you should use mysql_real_escape_string() . 如果你真的想使用不推荐使用的mysql_*函数,你应该使用mysql_real_escape_string()

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

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