Error: You have an error in your SQL syntax near - php

So I was getting this error then I enlosed the column names in the backtick character and the error was resolved, my query looked something like this
$sql="INSERT INTO camera (type, company, model, tvl, range, ir, mrp, price, warranty, image)
VALUES
('$ftype','$fcompany','$fname','$ftvl','$frange','$fir','$mrp','$price','$fwarranty','$pathAndName')";
And now it looks something like this and is working fine
$sql="INSERT INTO camera (`type`, `company`, `model`, `tvl`, `range`, `ir`, `mrp`, `price`, `warranty`, `image`)
VALUES
('$ftype','$fcompany','$fname','$ftvl','$frange','$fir','$mrp','$price','$fwarranty','$pathAndName')";
Now I figured out that I was getting this error because I was using some keyword in my query, my question is which keyword was causing this error, was it range? I tried the query without using the range column but still I was getting the same error.

You are doing the same mistake all new PHP users do - you aren't reading the error message.
For some reason you notice only the fact of the error, but read no description (and post no description as well).
While the text of the error message is a key.
Speaking of mysql errors - they include a part of SQL, starting after the erroneous spot.So - all you need is to look at the place right before the query part cited in the error message

Related

Excecuting mysql querys in wordpress

im using the insert php code plugin in wordpress and im trying to do inserts and mysql querys, but there's an error that appears me:
Error: INSERT INTO porschec_clientes.clientes(
ID,
NAME,
LAST_NAME,
EMAIL,
PHONE,
PORSCHE,
REFERENCE,
STATUS
CODE,
)
VALUES (NULL,’name’,’last_name’,’email#gmail.com’,’123123′,’911′,’name’, 0, ‘cdcc34cd554621097f9a6fdc3b2cc728′)
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 ‘CODE,
)
look that in "CODE" there's this quote symbol " ‘ " (i dont know if the correct translation is quote but... whatever haha) but in my php code i have it this way
VALUES (NULL,'name','last_name','email#gmail.com','123123′,'911′,'name', 0, 'cdcc34cd554621097f9a6fdc3b2cc728')";
wordpress is changing the quote symbol when i update the page, there's any way that i can avoid this? thank you
You missed a ,after STATUS, that's what the error tells you. Usually it shows the part after the error.
REFERENCE,
STATUS, <--
CODE,
Beside that let it change the quotes. That's all right.

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

Can't figure out source of mySQL syntax error

I'm working on a private messaging system between users on my site. Here's my query:
$query = "INSERT INTO messages (to, `from`, message) VALUES ('{$user}', '{$username}', '{$message}')";
However, 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 'to, `from`, message) VALUES ('Cheezey', 'Cheezey', 'Enter your message here')' at line 1
I have a nagging feeling that it's a really stupid error on my part, but I can't seem to figure it out.
That's because to is a reserved word in MYSQL, you have to put ` around it, like this:
INSERT INTO(`to`, ...).

PHP/AJAX issue inserting to mysql database

I'm having a problem inserting some data to a mysql database. I have used the same method with other features on the site, and this is the only one causing problems. It's meant to input into 3 field in the database (To, From, Message). As you can see it's a very basic messaging system.
I have the data coming into PHP via AJAX. But the problem is within the INSERT. I have messed around with it for over an hour now - no luck! Here is the code to insert:
mysql_query("INSERT INTO messages (To, From, Message) VALUES('$to','$loggedin','$message') ")
or die(mysql_error());
And here is the SQL 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 'To, From,
Message) VALUES('Ryan','Ryan','hhh')'
at line 1
I have tried adjusting a lot of things, no luck! :(
"TO" and "FROM" are reserved keywords, it's not wise to use them as column names. You have to escape them with a back-tick "`". Try this:
INSERT INTO messages (`To`, `From`, `Message`)
See the list with reserved words: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

php, MySQL and variables containing å, ä and ö escape problem

I'm having some problem with my SQL syntax/escaping variables on my LAMP server.
The command I want to use is the following:
$sql=mysql_query("INSERT INTO '$table' (FirstName, LastName, StartDate, TimeStroke, DueDate, Duration, Price, Retailer, Checksum)
VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[startdate]','$_POST[timestroke]','$duedate','$_POST[duration]','$price','$_SESSION[name]','$random')");
The problem is that sometimes the $table variable contains characters like å, ä and ö.
Hence I need to put ' ' around $table to make sure it stays the same. However when doing that recieve the 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 ''tablename' (FirstName, LastName, StartDate, TimeStroke, DueDate, Duration, P' at line 1".
Looks like the escaping by ' ' creates a problem.
I've tried with replacing the query with a mysql_real_escape_string:
"$sql=sprintf("INSERT INTO '".mysql_real_escape_string($table)."' (FirstName, [...]"
but that doesnt help me either.
Is there a way to keep the data in the variable intact and still be able to run the query? Or do I have to accept that å,ä,ö is banned from php/MySQL?
This is do to with character-encoding. Check out http://www.sitepoint.com/blogs/2006/03/15/do-you-know-your-character-encodings/
Put header('Content-Type: text/html; charset=utf-8'); at the top of your page
Also try doing mysql_set_charset('utf8'); before insert/reading from DB. Then you should put the following on your form that's posting to your PHP file:
<form action="/your-post-controller.php" method="post" accept-charset="utf-8">
Notice the accept-charset="utf-8 -- this is extremely important otherwise your header will report to the PHP file its in latin1
It should work then.
Also take a look at http://www.phpwact.org/php/i18n/charsets -- was trying to find the link, definitely worth a read for anyone interested in getting character encoding right, see the Iñtërnâtiônàlizætiøn string for testing your PHP&MySQL table
You would use backticks (`) to surround the table name. Nothing to do with Character-encoding:
$sql=mysql_query("INSERT INTO `$table` (FirstName, LastName, StartDate, TimeStroke, DueDate, Duration, Price, Retailer, Checksum)
VALUES ('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['startdate']}','{$_POST['timestroke']}','$duedate','{$_POST['duration']}','$price','{$_SESSION['name']}','$random')");
A couple of side notes here:
A: you should really user mysql_real_escape_string on any input coming in from an unknown source to avoid someone destroying your database with SQL Injection.
B: You should use ' around array associative indexes, reason being is that these all would throw notice undefined constant (or something to that matter) errors. Which will fill up your error log and make finding more critical errors a bit harder if you ever need to go back.

Categories