U
U
Username2015-04-21 14:45:15
PHP
Username, 2015-04-21 14:45:15

What should be the SQL query syntax in php?

Good afternoon. In phpMyAdmin I make a SQL query with the following content:
UPDATE `users` SET `name`='Vlad' WHERE 1
Next, I want to insert this query into a php file so that the user can make changes to his profile, naturally transforming it.
I have:
$query = mysql_query("UPDATE `users` SET `name`=".$name." WHERE ".$id."");
$name - New username
$id - user number in the database
Where is the incorrectness in the request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2015-04-21
@dalv_happy

where id = 1
where 1 is "update all"

F
FanatPHP, 2015-04-21
@FanatPHP

The SQL syntax is no different.
The only difference is the way of variable substitution.
Firstly, queries should be executed not through mysql_query, but through PDO
. Secondly, there should be question marks in place of variables , and the variables themselves should be passed to the request during execution:

$stmt = $pdo->prepare("UPDATE `users` SET `name`=? WHERE id=?");
$stmt->execute(array($name, $id));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question