Insert single numerical value into MySQL column [duplicate] - php

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
The query I'm trying to run is:
INSERT INTO albumtest (on) VALUES ('3')
Server type: MySQL
Server version: 5.6.21
Syntax 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 'on) VALUES ('3')' at line 1
I've been looking over this for almost a day now, and I can't seem to figure out why I'm getting this syntax error.
Full code is here: http://pastebin.com/6mMbZ1Y1
But I know the rest of it is fine, because it can run other queries fine (such as $sql = "INSERT INTO gallery (title) VALUES ('".$title."')";)

this is the correct syntax
INSERT INTO `albumtest` (`on`) VALUES ('3')

ON is reserved keyword of mysql it should be write inside backtics
INSERT INTO albumtest (`on`) VALUES ('3')

on is MySql reserved keyword. Dont use it as column name.

Related

PHP PDO Exception[42000] when Deleting and Inserting into table in one statement [duplicate]

This question already has answers here:
PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)
(7 answers)
Closed 8 months ago.
I'm trying to delete any existing rows with a specified column value if they exist and Insert a new row directly after in a single prepared statement. I have tried both binding param values and passing an array to execute();
$sql = "DELETE FROM table WHERE id = :id;
INSERT INTO table (col1,col2,col3,col4,col5) VALUES (:col1,:col2,:col3,:col4,:col5);";
I have also tried:
$sql = "DELETE FROM table WHERE id = ?;
INSERT INTO table (col1,col2,col3,col4,col5) VALUES (?,?,?,?,?);";
They work fine separately, but wont work in one statement. The exact error being thrown is:
PHP Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO ...
The syntax error is pointing to the start of the second statement, so is it possible that I'm not supposed to be running a delete and insert statement after each other? I only have one version of mysql/mariadb installed and it's always been up to date, and the syntax works in separate statements. Operating system is Windows.
#Don'tPanic commented a link with a good answer:
PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)
In my Database config model I had set the ATTR_EMULATE_PREPARES to false for some reason, and setting it to true fixed the issue and the statement executed just fine. The post in the link also advises to specify the encoding type in the dsn if you do choose to set the attribute to true.

SQL syntax error with no known mistakes in query [duplicate]

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 5 years ago.
I'm getting a syntax error for a very simple SQL query I'm trying to do:
INSERT INTO history (character, type, amount, extra)
VALUES('$character', '$type', '$amount', '$extra')
Here's the way the table is set up:
SQL table
The full error it gives me is the following:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'character, type, amount, extra) VALUES('Ellie', 'Gift', '-200', 'to Rick')' at line 1
I've already checked and double checked the usual mistakes like the table name, spelling errors, column order etc, but I'm clueless as to what it's still detecting, and hoping one of you can help me out...
character is a reserved keyword in mysql. Rename the column or use backticks for escaping it.
INSERT INTO history (`character`, type, amount, extra)
VALUES('$character', '$type', $amount, '$extra')

Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 5 years ago.
I'm really confused right now, keep getting this MySQL error:
Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'condition='-78' WHERE id='2'' at line 1
While I KNOW that condition is correct, when I remove the condition='".$data['condition']."' from the update record, the update is being processed but when I add that, it fails.
The field in the database is VARCHAR (6), just like all the other fields.
What could be the issue?
condition is a MySQL keyword. You need to enclose it with backticks if you're using it as a column name.
`condition`='-78' WHERE id='2'
Take care: This is the backtick symbol, not the single quote.
condition is a reserved word in MySQL.
You can see the full reserved word list here
You need to wrap condition in backticks;
"UPDATE ..... WHERE `condition`= '".$data['condition']."'";
A note for next time: Please include as much code as is sensible next time. Otherwise it is trickier for people to help you.

PHP insert into SQL statement with several parameters [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
i have a simple php INSERT INTO SQL statement that simply refuses to update several columns at once. i have no idea why but the following statement is acceptabel;
$sql = "INSERT INTO niceTable (first) VALUES ('Hello')";
however if i try to following
$sql = "INSERT INTO niceTable (first, last) VALUES ('Hello', 'You')";
it breaks down and throws the following error:
"Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'desc) VALUES ('Hello', 'update')' at line 1"
I have checked the syntax, but it seems ok. I am using a one.com server. Anyone got any tips?
Your actual query (not the one in your question) seems different. The error message seems to have desc somewhere, which is a reserved word. If you use reserve words as column names (don't), you should enclose them in backticks:
INSERT INTO tbl (`order`, `desc`) VALUES ('foo', 'bar');
As per your "posted code":
The reason being that first and last are MySQL reserved words
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
and require special attention.
Either wrap them in ticks or rename them to something other than reserved keywords.
INSERT INTO niceTable (`first`, `last`)
Edit: However, your error doesn't support the issue here, nor the column name(s):
for the right syntax to use near 'desc)
this tells me you are using desc which is also another MySQL reserved word.
You should also use prepared statements
https://en.wikipedia.org/wiki/Prepared_statement
Plus, should your inputs contain characters that MySQL may complain about such as apostrophes John O'Neil then you will need to escape those values.
MySQL will interpret that as ('Hello', 'John O'Neil') in turn causing another syntax error.
Escaping it, would interpret it as ('Hello', 'John O\'Neil') making it valid.
I'm thinking ahead here.
Enclose your column names in backticks
Last is a function in MySQL
$sql = "INSERT INTO niceTable (`first`, `last`) VALUES ('Hello', 'You')";

Unable to insert a record in db in PHP [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I am new user in php. I am trying insert in a table using following query:
$insert = "INSERT INTO forget (key,user_name) values('Abc','Xyz')";
mysql_query($insert)
echo mysql_error();
Output:
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 'key,user_name) values('abc','xyz')' at line 1
MySQL has a few reserved words which will cause queries using them to fail. In this case it is key.
You can either change the column name (also known as "key" which is why it fails) or you can escape the term with backticks like so:
$insert = "INSERT INTO forget (`key`,user_name) values('Abc','Xyz')";
Here are the list of words what mysql has reserved, and that cant be used as a table or field name in a query, unless its escaped using back ticks. You can use them as your field name, but when you query it, it must be escaped using " ` ".
In your case, key is a reserved word. So you must either escape it using back ticks.
Here is a list of mysql reserved words : https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Categories