PHP insert into SQL statement with several parameters [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.
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')";

Related

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')

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

What is wrong with my SQL/PHP syntax here?

So I have a database of downloads on my site set up that enables me to track the number of downloads, and I'm trying to set up a front-end for me and my compatriots to insert new downloads into the database. I'm setting up the front-end with primarily PHP.
The way my paging is set up removes the possibility of my forms simply posting, so instead I have JS serializing the data and reloading the page, then I unserialize the data in PHP, stick the values into a mysql query, and try to run it.
Here's what my SQL code looks like inside of PHP:
$sql = "INSERT INTO dl (id, file, desc) VALUES ('$idd', '$file', '$desc')";
Which turns into this string:
INSERT INTO dl (id, file, desc) VALUES ('a56', 'test.zip', 'cake')
But when the page tries to run it, I get this 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 'desc) VALUES ('a56',
'test.zip', 'cake')' at line 1
And the weirdness of that is compounded by the fact that the line of code running the query is not on line 1. It's on line 28.
Any help is appreciated :)
desc is a reserved keyword in MySQL.
The recommended workaround is to use backticks. From MySQL manual:
If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.
If renaming the table or column isn't possible, wrap the offending identifier in backticks (`):
$sql = "INSERT INTO dl (id, `file`, `desc`) VALUES ('$idd', '$file', '$desc')";
Take a look at this question too: When to use single quotes, double quotes, and backticks in MySQL
Please change the column name of desc, its reserved by MYSQL. for more details please see list of reserved words on MYSQL Reserved Words
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
desc is a reserved word in SQL.
It is (together with ASC) used to determine the sorting order of the results.
Please try this.
$sql = "INSERT INTO dl (`id`, `file`, `desc`) VALUES ('".$idd."', '".$file."', '".$desc."')";
hope this is your useful.

PHP syntax error adding data to MySQL db

trying to add data to mySQL db.
I get this 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 'MATCH(time, date, location, weather, team_id) VALUES('t', 't', 't','t','2')'
this is the PHP code snippet:
$sql = "insert into MATCH(time, date, location, weather, team_id) VALUES('$time', '$date', '$location','$weather','$team_id')";
I cant see any syntax errors
MATCH is a reserved for a function used in fulltext search:
http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
That's not a php syntax error. It's a Mysql syntax error. I suggest changing the table's name.
Try
$sql = "INSERT INTO `MATCH` (`time`, `date`, `location`, `weather`, `team_id`) VALUES ('".$time."', '".$date."', '".$location."','".$weather."','".$team_id."')";
Using the backtick character ` you can distinguish names you gave to your table or columns from reserved words of the MySQL language. Leaving them out might seem more compfortable at first, but can be a pain later.
E.g. one should know that mysql syntax is not case sensitive. So even if you write match you will get this problem. A list of the reserved words can be found at the link Mark gave you in his comment.
You might also want to read up on MySQL Syntax in general:
http://dev.mysql.com/doc/refman/5.1/en/sql-syntax.html

Sql with mysql reserved word [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I have this following sql code:
$sql = "INSERT INTO data (Artist, Name) VALUES ('TF2', 'you're right behind me')";
The code itself looks normal but for some reason mysql doesn't want to allow me to save it. I get the following error:
"#1064 - 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 're right behind me')' at line 1"
What I know the problem is because of the word "right" being reserved in mysql but I need to save it so how should my code look like. All help is appreciated
As #Fred and #JunM have already commented, you have two issues. The first is that Name is a reserved word. The second is that you have a single quote inside your single quoted string. Change your SQL to this:
$sql = "INSERT INTO data (`Artist`, `Name`) VALUES ('TF2', 'you\'re right behind me')";
Your problem is because you have an ' in the work you're. So your string is terminating to early in your sentence. Use you\'re instead to escape the character '
$sql = "INSERT INTO data ('Artist', 'Name') VALUES ('TF2', 'you\'re right behind me')";
My experience with MySQL is limited, but I use SQL Server extensively. To me it seems that the problem is in the apostrophy used in the "you're right behind me". In SQL server, I'd have to use a double apostrophy, so the sql instruction would be something like this (notice the double apostrophy in the you''re):
$sql = "INSERT INTO data (Artist, Name) VALUES ('TF2', 'you''re right behind me')";
Hope this helps.
Regards

Categories