sql syntax error using variable's value as column [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I'm trying to update a value in my table, but i have a syntax-error and i can't find the error.
This is my php code:
$data = new MysqlClass();
$data->connect();
$result_sql = $data->query("UPDATE iscrizioni SET '".$matricola."' = 'si' WHERE 'COD'=".$cod);
it returns me:
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 ''805710' = 'si' WHERE 'COD'=1' at line 1
Have you any idea where the error is?
Thank you very much!

You are using column's names as strings, use ` instead of '. Or don't use it at all.
$result_sql = $data->query("UPDATE `iscrizioni` SET `".$matricola."` = 'si' WHERE `COD` = ".$cod);

Related

Unable to use dynamic variable as column name in php mysql [duplicate]

This question already has answers here:
Are you allowed to use numbers as table names in MySQL?
(5 answers)
Closed 5 years ago.
Am trying to use dynamic column name in php mysql update but am getting error
Here is code
$time=date("H");
$video_view = 234
$update_query = "UPDATE videos SET ". $time . "= {$video_view} WHERE id={$id}";
Here is the error
UPDATE videos SET 14= 200079 WHERE id=1Query failedYou have an error in
your SQL syntax; check the manual that corresponds to your MariaDB
server version for the right syntax to use near '14= 200079 WHERE id=1' at line 1
First of all you should really use prepared statements and bound parameters.
If your column really got the name '14' like in the variable $time then you can try this
$update_query = "UPDATE videos SET `". $time . "` = {$video_view} WHERE id={$id}";
So far as I know column names should stand between `` because of reserved names like numbers or function names.
I would avoid it because it will make those errors und I don't know if the query does make sense

error in MySql syntax near = in where clause [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
i am writing this command
$result = mysql_query("SELECT * FROM register where name= ‘lakhan’ ") or die(mysql_error());
and when running the php file it shows:
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 '‘lakhan’'
I am using MySql Server 5.5 AND INSTEAD OF = sign i have used LIKE also in query then too error is coming.Please help me to resolve it
Use single quotes arround the value:
$result = mysql_query("SELECT * FROM register where name= 'lakhan' ") or die(mysql_error());
Do not longer use the deprecated mysql_* API. Use mysqli_* or PDOwith prepared statements.

why am i getting this query failed syntax error [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I can not find out why this is not working.....
i keep getting this message
Query failed: 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 ''laketaho_benny'.'tblPictures' SET 'caption' = 'uuuuuuuuuupppp' WHERE 'tblPic' at line 1
and this is my code
$sql = "UPDATE `laketaho_benny`.`tblPictures` SET `caption` = `$caption` WHERE `tblPictures`.`pictureID` =$pictureID;";
Single quotes are for strings. You're thinking of backquotes ( ` ).

php variable used as mysqli column name syntax error [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 9 years ago.
This is where I think it´s the problem...
$sql1 = "SELECT `puntos_globales`, '$juego'
FROM `lista_jugadores` WHERE `id_jugador`='$noTop'";
This is the error message:
Error:
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 "blackOps2'='1' WHERE `id_jugador` = '10" at line 1
$juego .. is a php variable that holds the column name; in this case blackOps2. I don´t know why in the error says blackOps2'='1'??
from the error it seems you didnt provide the true query and it looks you have two where clause
blackOps2='1' WHERE `id_jugador` = '10"
try do it like that
WHERE `id_jugador` = '10' AND blackOps2='1'

Bad SQL syntax? [duplicate]

This question already has an answer here:
How to insert into MySQL using a prepared statement with PHP [duplicate]
(1 answer)
Closed 9 years ago.
I'm getting the following error:
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 '' DStormr', 'ddo.png', 'Online:' at line 1`
The sql is the following:
"UPDATE articulo SET '".$nombre."', '".$imagen."', '".$text."', '".$precio."', '".$popup."', ".$genero_id.
" WHERE id=".$id"";
What am I missing/not seeing?
When you do an UPDATE you need to SET key = 'value'.

Categories