简体   繁体   English

MySQL UPDATE语法错误-一切正常吗?

[英]MySQL UPDATE syntax error - Everything is ok?

I've got an error with my update query in PHP... I've seen other people's mistakes, and I'm almost certain I'm not making the same old mistakes, but I may be ignoring one. 我的PHP更新查询中有一个错误...我已经看到其他人的错误,并且几乎可以肯定我没有犯同样的旧错误,但是我可能会忽略一个错误。

This is my code: 这是我的代码:

$sQuery = "UPDATE clientes 
        SET 
          Nombre = '$_POST[Nombre]',
          Apellidos = '$_POST[Apellidos]',
          Telefono = '$_POST[Telefono]',
          Email = '$_POST[Email]',
        WHERE ID= $sIDCliente";

First I thought it had a problem with the $_POST's, but when I echo'ed the query, it was allright. 首先,我认为它与$ _POST的问题有关,但是当我回显查询时,它就可以了。 The error I get is this one: 我得到的错误是这个:

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 ID= F17DEF774C' at line 7

Well, that's what the page outputs. 好吧,这就是页面输出的内容。 Thank you all before hand :) 先谢谢大家:)

You have an extra comma in the row 该行中有一个逗号

Email = '$_POST[Email]',

should be 应该

Email = '$_POST[Email]'

edit: 编辑:

Also I should mention that you are better off using parameterized queries, and then binding the parameters. 我还要提到,最好使用参数化查询,然后绑定参数。 It makes your database transactions more secure. 它使您的数据库事务更安全。

So in your case it would look like this 所以在你的情况下,它看起来像这样

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
$stmt = $mysqli->prepare("
        UPDATE clientes 
        SET 
          Nombre = ?,
          Apellidos = ?,
          Telefono = ?,
          Email = ?
        WHERE ID= ?
        ");
$stmt->bind_param('ssssd', $_POST[Nombre], $_POST[Apellidos], $_POST[Telefono], $_POST[Email], $sIDCliente);
$stmt->execute();

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

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