SQL Query not working - No reason? [duplicate] - php

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I have a problem. I created a SQL Query, to insert something into the database:
INSERT INTO order (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending)
VALUES ('232784', 'Niklas Peters', 'Some Stret', 'PostalCode', 'Wien', 'AT', 'email#email.com', '1454593142', '1')
But I always get the 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 'order
(kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending)
' at line 1
My Code is PHP
$sql = "INSERT INTO order (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending) VALUES ('".$kdnr."', '".$emp."', '".$adresse."', '".$plz."', '".$ort."', '".$land."', '".$email."', '".$time."', '1')";
I just dont know what is wrong - Am I blind?
Cheers - would be glad for help!

Quote order with backticks:
INSERT INTO `order` (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending)
VALUES ('232784', 'Niklas Peters', 'Some Stret', 'PostalCode', 'Wien', 'AT', 'email#email.com', '1454593142', '1');
ORDER (R) is reserved word.
Reserved words are permitted as identifiers if you quote them

Your table name matches a reserved word, so will need to be quoted in SQL queries, e.g
INSERT INTO `order` (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending) VALUES ('232784', 'Niklas Peters', 'Some Stret', 'PostalCode', 'Wien', 'AT', 'email#email.com', '1454593142', '1')
NB. When calling this from PHP, you may want to consider using prepared queries a la PDO, etc.

Related

Can't insert data into my local mysql server [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
Hi I'm having problems inserting into my local database, I have an established connection as I can query using "SELECT * FROM contact", but I cannot insert using the following code
$query1 = "INSERT INTO 'contact' ('e-mail', 'subject', 'body')
VALUES('email#gmail.com', 'another question', 'Just asking?')";
This is the error it's giving me -
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 ''e-mail', 'subject', 'body')
If anyone could kindly assist?
Try this. no need for ' around table name
$query1 = "INSERT INTO contact (`e-mail`, `subject`, `body`)VALUES('email#gmail.com', 'another question', 'Just asking?')";

Why is my Query wrong and the phpMyAdmin's is not? [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 made a query like this:
INSERT INTO slider ('h1', 'h2', 'data-bg', 'data-img', 'data-url', 'status') VALUES ('Lalala', 'Lalala', 'http://localhost/mares.com.br-final/images/backgrounds/black_thumb.jpg', 'http://localhost/mares.com.br-final/images/banners/papeis_2015.png', 'http://lalala.com', 1)
But it's not working and phpMyAdmin says the syntax is incorrect.
So I tried adding a line through phpMyAdmin and it gave me back a query, I cleaned it, to take away some elements that I'm not inserting in my query, and tried it, and it works...
Here's the phpMyAdmin's query:
INSERT INTO slider (`data-img`, `data-bg`, `data-url`, `h1`, `h2`, `status`) VALUES ('images/lalal.jpg', 'images/back/lalala.jpg', 'http://lalal.com', 'Oi', 'Olá', '1')
Obs: I alredy tried to change ' for ` (the symbol to add code here) int he columns names.
The querys are identical to me, so why my query is wrong and phpMyAdmin's is not?
Your using single quotes for column which is wrong, instead of using single code for column use Backticks.
wrong syntax : ('h1', 'h2', 'data-bg', 'data-img', 'data-url', 'status')
right syntax : (`data-img`, `data-bg`, `data-url`, `h1`, `h2`, `status`)
for more refer the below link When to use single quotes, double quotes, and backticks in MySQL

Error in PHP MySQL

I am trying to do a simple INSERT, but I keep on getting 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 'FROM,TO,ID,CURRENCY1,CURRENCY2,AMOUNT,NOTE,RATE) VALUES('test', 'test2', 'dd', '' at line 2
Here is my code:
mysql_query("INSERT INTO WIRET
(FROM,TO,ID,CURRENCY1,CURRENCY2,AMOUNT,NOTE,RATE) VALUES('$from', '$to', '$ID', '$currency1', '$currency2', '$amount','$note', '$rate') ")
or die(mysql_error());
Why am I getting this error? I copied this script from another area of my site where it works, I just changed the values.
FROM is a reserved word in MySQL (and SQL in general). If you really have a column named FROM you should wrap it with ` (backticks) so the parser knows you mean a name:
INSERT INTO WIRET (`FROM`, TO, ID, CURRENCY1, ...
If your column is named from you have to put it into "`" (backticks) because FROM is also a SQL keyword.
By putting a keyword (FROMhere) into backticks you say "this is not a SQL keyword" to the DBMS.
Example:
INSERT INTO WIRET (`FROM`,TO,ID,...

Php mysqli->real_escape_string and MYSQL

I've a little question.
I've written this code to add the values to mysql database but when i run the code and I got an error. Can anybody help me?
the code:
$fel = $mysqli->query("INSERT INTO deleted (uid,buy_type,prop_type,district,street,room_min,room_max,price_min,price_max,condition_type,heat_type,lift_type,parking_type,type_of_del,when)
VALUES ('".$mysqli->real_escape_string($letomb['uid'])."',
'".$mysqli->real_escape_string($letomb['buy_type'])."',
'".$mysqli->real_escape_string($letomb['prop_type'])."',
'".$mysqli->real_escape_string($letomb['district'])."',
'".$mysqli->real_escape_string($letomb['street'])."',
'".$mysqli->real_escape_string($letomb['room_min'])."',
'".$mysqli->real_escape_string($letomb['room_max'])."',
'".$mysqli->real_escape_string($letomb['price_min'])."',
'".$mysqli->real_escape_string($letomb['price_max'])."',
'".$mysqli->real_escape_string($letomb['condition_type'])."',
'".$mysqli->real_escape_string($letomb['heat_type'])."',
'".$mysqli->real_escape_string($letomb['lift_type'])."',
'".$mysqli->real_escape_string($letomb['parking_type']).",
'".$mysqli->real_escape_string($type_of_del)."',
now())") or die($mysqli->error);
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 'when) VALUES ('3', 'kiado', 'lakas', '1'' at line 1
WHEN is a reserved word. Enclosing it in backticks should fix your problem, as it will then be treated as an identifier.
`when`
You should use backticks around your column names. when is a MySQL keyword so it's being interpreted incorrectly. At the very least use backticks around when.

MySQL Syntax Error; "check the manual that corresponds to your MySQL server version.." [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 8 years ago.
I've been looking all over the internet for a solution to 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 'primary, username, password, password2) VALUES (null, 'hello', 'hello', 'hello')' at line 1"
I have no idea what is going on.. I know you will ask what my code is so here:
$con = mysql_connect("localhost","root","*****");
if (!$con)
{
die('Server overload, please try again' . mysql_error());
}
mysql_select_db("users", $con);
$sql = "INSERT INTO details (primary, username, password, password2) VALUES (null, '$_POST[username]', '$_POST[password]', '$_POST[password2]')";
if (!mysql_query($sql,$con))
{
die('Error: Server overload, try again' . mysql_error());
}
echo "You have signed up successfully!";
mysql_close($con);
I've been trying to figure it out for around 4/5 hours now and have had no success.
Thanks,
Lawrence
primary is a reserved keyword, in SQL, which means that you should either :
rename that column -- would be a good idea, to avoid that kind od situation
or use backticks arround that name
Here what the query would look like in the second case :
INSERT INTO details (`primary`, `username`, `password`, `password2`)
VALUES (null, 'hello', 'hello', 'hello')
Note : and you should escape your values, using mysql_real_escape_string, to avoid SQL Injections !
Try not to name your tables or columns with relitively common names like primary and details.
While they may not be reserved words in the flavor of SQL you are currently using, you never know when you might be supporting other types (Postgres, Oracle, etc.).
You can also use this handy-dandy reserved word checker.
Followup Question:
I would like to know who wrote the error statement you are getting, which essentially says RTM? Hilarious. I'm going to use that in my next try catch. :)
Primary is a reserved word. What is the table definition?
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
I'd rename that first column to something else: "primary" is a reserved word in MySQL:
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

Categories