MySQL Error - Inserting into Table - php

I have a little problem inserting into a table, here is the code:
mysql_query("INSERT INTO admin_menu (id, title, type, icon, parent, url, order, append, module) VALUES('', 'powerpoint', '0', 'powerpoint.png', '0', 'powerpoint/config', '0', '0', '0' ) ") or die(mysql_error());
it gives me 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 'order, append, module) VALUES('', 'powerpoint', '0',
'powerpoint.png', '0', 'pow' at line 1
Any help would be greatly appreciated, thanks!

order is a reserved word. Wrap it in backticks
... url, `order`, append,...
You also shouldn't be using mysql_query.
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.

you need to use backticks. order is reserved keyword.
mysql_query("INSERT INTO admin_menu (id, title, type, icon, parent, url, `order`, append, module)

Related

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

1064 syntax error in mysql

I am confused. I don't know what is the error. On execution I got a message:
" 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(creator,type,name,details,icon)values (6,'information
sharing','test','j' at line 1"
my query is :
INSERT INTO group
(creator, type, name, details, icon) VALUES
(6, 'information sharing', 'test', 'just for testing',
'My friend/group_uploads/pic00_6_0d46839f6371fb84f6b6c682f5fc2c77.jpeg')
this my table specification:
type varchar(1000)
name varchar(1000)
details varchar(1000)
creator bigint(20)
icon varchar(1000)
please help me to correct the error .
group is a reserved word in MySQL. You need to surround it in backticks.
Like this..
insert into `group`(creator, type, name, details, icon) values ('6','information sharing','test','just for testing','My friend/group_uploads/pic00_6_0d46839f6371fb84f6b6c682f5fc2c77.jpeg')
^ ^
Try to avoid having such names for your columns and tables.
Grou is Keywords in MySQL. So use backticks for those.
Try:
INSERT INTO `group`
(`creator`, `type`, `name`, `details`, `icon`) VALUES
(6, 'information sharing', 'test', 'just for testing',
'My friend/group_uploads/pic00_6_0d46839f6371fb84f6b6c682f5fc2c77.jpeg')
Even though by using backticks you can use keywords as table name , It is not recommended to use reserved word as table name
DO NOT DO IT.
Official list of reserved keywords: Reserved Keywords (Transact-SQL)

MYSQL error regarding syntax

I keep getting 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 'exit, openclosed, longshort, target_one, target_two,
target_three, notes, entryd' at line 1
For this php script that I'm trying to run from MYSQL.
$sql = mysql_query("INSERT INTO stockpicks (symbol, entry, exit, openclosed, longshort, target_one, target_two, target_three, notes, entrydate)
VALUES('$symbol','$entry','$exit','$openclosed','$longshort','$target_one','$target_two','$target_three','$notes',now())") or die (mysql_error());
The problem is I see no error. I've checked both this particular line and the lines surrounding. For example I re did the '$var' section which has given me trouble in the past, but that doesn't seem to be the issue. My table structure is as follows
id int(11)
symbol varchar(255)
entry varchar(255)
exit varchar(255)
openclosed varchar(255)
entrydate datetime
longshort varchar(255)
target_one varchar(255)
target_two varchar(255)
target_three varchar(255)
exit is a reserved word. If you want to use it as a column name, quote it in backticks:
`exit`
Try this,
$sql = mysql_query("INSERT INTO stockpicks (symbol, entry, `exit`, openclosed, longshort, target_one, target_two, target_three, notes, entrydate)
VALUES('$symbol','$entry','$exit','$openclosed','$longshort','$target_one','$target_two','$target_three','$notes',now())") or die (mysql_error());
$pid = mysql_insert_id();
Note that escaping your identifiers in MySQL way using backticks decreases portability on a plain place.
I would either use double quotes with ANSI SQL mode enabled, or just give my variables names which are unlikely to be become reserved in future.
exit is a reserved word in mysql.

SQL Not null and php = or false

I have a loop that inserts descriptions and thumbnails into a table, but not all the items in the loop have descriptions and thumbnails. I didn't think this was a problem, but sql won't insert it, unless they have a value. I thought it might be cause by the not null thing, so i tried to change "null" to "yes".
I set it to print out the executed query and the mysql error:
Query: INSERT INTO experiments (title, dir, desc, thumbnail) VALUES('3dbox', '3dbox', '', '')
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, thumbnail) VALUES('3dbox', '3dbox', '', '')' at line 1
I've also tried this:
$d = #file_get_contents("/experiments/$sites[$i]/desc.txt") or false;
but that doesn't work either, as you can see in the query.
desc is a reserved word in Mysql. Hence, the error. You would need to change the column name to something else, or access it with backticks like this,
INSERT INTO experiments (title, dir, `desc`, thumbnail) VALUES('3dbox', '3dbox', '', '')
Here's the manual for reserved words.
desc is reserved in mysql and will have to be identified as:
INSERT INTO experiments (title, dir, `desc`, thumbnail) VALUES('3dbox', '3dbox', '', '')
You can not use "desc" as a column name because is a reserved word: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html, you need to escape it: try changin your query to this:
INSERT INTO experiments (title, dir, `desc`, thumbnail) VALUES('3dbox', '3dbox', '', '');

ERROR When trying to insert into MySQL table with PHP

I don't know what's wrong with my syntax, but I'm missing something:
$createrequest = mysql_query("INSERT INTO products_updates_queue (id, kid,
product_version_id, key, ip) VALUES ('$request_id', '$uid', '$version_id',
'$request_key', '$request_ip')");
I receive 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 'key, ip) VALUES ('j4ctveyd0x62', '1', 'z451ah3', 'hqbyu7bhg8za', '64.134.163.2' at line 2"
Can anyone see what I am missing?
I think key is a reserved word, and you should avoid using it as a column name. Try using backticks around it:
$createrequest = mysql_query("INSERT INTO products_updates_queue (id, uid, product_version_id, `key`, ip) VALUES ('$request_id', '$uid', '$version_id', '$request_key', '$request_ip')");
key is a reserved word in MySQL. Avoid it, or wrap it in backticks.
Edit: And I hope you escaped the variables you're putting into that query.

Categories