PHP syntax error adding data to MySQL db - php

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

Related

MySQL DB query throws error saying I have syntax errors. Can't seem to find them? I believe it's in the columns portion of the query

I'm creating raw MySQL queries to insert into my DB using the Laravel 'DB' facade. I've gotten this same type of query to work with another database table, but it's throwing errors on this specific one.
1 PDOException::("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 'group,originating_attorney,practice_area,responsible_attorney,statute_of_limitat' at line 1")
/home/vagrant/Code/proj/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452
2 PDO::prepare("INSERT INTO firm_matters(id,etag,display_number,custom_number,description,status,location,client_reference,billable,maildrop_address,billing_method,open_date,close_date,pending_date,client,contingency_fee,custom_rate,group,originating_attorney,practice_area,responsible_attorney,statute_of_limitations,user,account_balances,custom_field_vals,custom_field_set_associations,created_at,updated_at) VALUES (...)
I've checked the reserved keywords for MySQL and I don't think I'm violating any rules. Could someone more versed in MySQL check to see if my query is correct.
I've checked the reserved keywords for MySQL
No you didn't
syntax to use near 'group,originating
^^^^^ here
GROUP is reserved word https://dev.mysql.com/doc/refman/5.7/en/keywords.html
simply don't create "raw MySQL queries" and you'll be fine next time. you are smart enough to use framework yet you use "raw MySQL queries". just don't do it.
check the manual that corresponds to your MySQL server version for the right syntax to use near 'group, ...
Here's a tip about MySQL syntax errors: When the SQL parser encounters a word in your query that confuses it, the error message shows you exactly where it got confused. In this case, the word group is the one that the parser wasn't expecting in that place in the query.
Others have answered that group is a reserved word in MySQL. You can use reserved words as identifiers (table names, column names, etc.) if you put them in back-ticks to delimit them and make it clear they are identifiers, not SQL keywords.
INSERT INTO firm_matters(id, etag, display_number, custom_number, description,
status, location, client_reference, billable, maildrop_address, billing_method,
open_date, close_date, pending_date, client, contingency_fee, custom_rate,
`group`,
originating_attorney, practice_area,responsible_attorney, statute_of_limitations,
user, account_balances, custom_field_vals, custom_field_set_associations,
created_at, updated_at) VALUES (...)
The recommendation from #Peter to use a framework is because many frameworks automatically delimit ALL identifiers in back-ticks, just in case they are reserved words.
But you can resolve this error yourself without adopting a complex framework. Either delimit your identifiers, at least those that match MySQL reserved words, or else don't use reserved words as identifiers in the first place.
There's nothing wrong with writing SQL directly.
depends on your MySQL version, at least group is reserved keyword
https://dev.mysql.com/doc/refman/5.7/en/keywords.html#keywords-5-7-detailed-G
when you write a raw query, change all the
column to `column`

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

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.

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

Categories