Can't figure out source of mySQL syntax error - php

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`, ...).

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.

Syntax error or access violation using OUTPUT keyword

I'm attempting to make use of the OUTPUT line so I don't have to use multiple queries to get information that I've inserted into a table. This is for a basic image uploader project that will rename the images based on their id in the database.
Here's the query I'm using (Using it with PDO)
INSERT INTO `images` (`id`, `type`, `category`, `title`) VALUES (null, :type, :cat, 'Newly uploaded image') OUTPUT INSERTED.id;
-
Processing upload
Error uploading file: SQLSTATE[42000]: Syntax error or access violation: 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 'OUTPUT INSERTED.id' at line 1
I've read over a few different tutorials now and I'm really confused as to what the problem is.
OUTPUT is not working in MYSQL.
Use
SELECT LAST_INSERT_ID();
after your insert query

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

Error: You have an error in your SQL syntax near

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

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

Categories