Héctor Salazar
Héctor Salazar

Reputation: 155

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.

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. 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 :)

Upvotes: 0

Views: 80

Answers (1)

echochamber
echochamber

Reputation: 1815

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();

Upvotes: 5

Related Questions