PHP - Insert SQL Error [duplicate] - php

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 6 years ago.
Database layout:
rid (auto increment) (primary key) (255)
song (varchar) (120)
artist (varchar) (30)
by(varchar) (33)
key(varchar) (60)
PHP code:
$sql = "INSERT INTO Requests (song,artist,by,key)
VALUES ('$song','$artist','$by','$key')";
if($this->db->query($sql))
{
die("true");
}
echo 'false: ' . $this->db->error;
Error:
false: 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 'by,key) VALUES ('testsong','testing','kyle','example')' at line 1
Help? I have debuged for ages, I can't see whats wrong with that SQL? Thanks in advance!

You need to use backtick for BY and KEY columns, both are mysql reserve words
$sql = "INSERT INTO Requests (`song`,`artist`,`by`,`key`)
VALUES ('$song','$artist','$by','$key')";
MYSQL Reserve Words List
Side Note:
I suggest you that, please do not use reserve words and keywords for table or column names.

You'll need back ticks for SQL-related names, also by and key are MySQL reserved words:
INSERT INTO Requests (`song`,`artist`,`by`,`key`)

Try this query to insert data in Requests table
$sql = "INSERT INTO Requests (`song`,`artist`,`by`,`key`)
VALUES ('".$song."','".$artist."','".$by."','".$key."')";
if ($this->db->query($sql)) {
die("true");
}
echo 'false: ' . $this->db->error;
the main problem in the asked question's query is apostrophe on both side of column name is missed as (song, artist, by, key) Should be ('song', 'artist', 'by', key')

Related

Is there any other way to delete rows from multiple tables without creating foreign key between tables? [duplicate]

This question already has answers here:
How to delete from multiple tables in MySQL?
(7 answers)
Closed 2 years ago.
This code does not work. Without creating foreign key between tables how can I delete data from multiple tables that matches the conditions? How can I write a 2 query and send it with PHP-PDO
$query = "DELETE FROM category, bookmark WHERE (bookmark.category = ? AND category.name = ?)";
$stmt = $db->prepare($query);
$stmt->execute([$categoryName, $categoryName]);
I am using "?" to prevent from sql injection.
This is the error I get.
JavaSQLSTATE[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 'WHERE
(bookmark.category = 'Java' AND category.name = 'Java')' at line 1
In multi-table DELETE you MUST specify the tables which records must be deleted:
DELETE category, bookmark
FROM category, bookmark
WHERE (bookmark.category = ? AND category.name = ?)
See MySQL 8.0 Reference Manual / ... / DELETE Statement, section "Multiple-Table Syntax". The names of tables which records must be deleted are NOT enclosed into square brackets as optonal (2nd row of query text in both syntax variants).

MySQL CREATE TRIGGER using php [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I have read some other posts like mine, but none of them solve my problem,
I have two tables, user and purchase, When a user signup, I want to add the same user_id to the purchase table as well. I try to right a php code for that like this:
$query = "CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)";
$result = mysqli_query($connection, $query);
if($result){echo "<br>TRIGGER Success!";} else {die("<br>Database query failed. " . mysqli_error($connection));}
this is the error I get:
Database query failed. 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 '' at line 1
I'm sorry if the question is repetitious.
It should be back tick not single quotes
CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)
UPDATE
Try this
DELIMITER$$
CREATE TRIGGER `purchase_insert`
AFTER INSERT ON `user`
FOR EACH ROW
BEGIN
INSERT INTO purchase (user_id) VALUES (NEW.user_id);
END$$
DELIMITER ;

PHP SQL syntax error for insert into

I have a bunch of php files corresponding to an application I am writing, using MySQL for my database structure. I know this questions has been asked before but I've been through most of the posts about it and can't find something that will help...
In my PHP file I have a SQL query
$group_sql = "INSERT INTO group (name, description, ownerEmail) VALUES ('$groupName', '$descrip', '$owner')";
that corresponds to a group table with three attributes: name, description, and owner email. $groupName, $descrip, $owner are three variables I have defined. I'm getting this syntax error when I try to run the query:
Error: INSERT INTO group(name, description, ownerEmail) VALUES(hi, hi, test#example.com)
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(name, description, ownerEmail) VALUES(hi, hi, test#example.com)' at line 1
Can someone please help me see what I'm doing wrong?
GROUP is a MySQL reserved keyword. If you name a table that, then you must wrap it in ticks:
$group_sql = "INSERT INTO `group` (name, description, ownerEmail)
VALUES ('$groupName', '$descrip', '$owner')";
Notice where SQL starts with the error and points to it?
>for the right syntax to use near 'group
> ^
This applies to both tables and columns.
Consult: http://dev.mysql.com/doc/refman/5.0/en/identifier-qualifiers.html

PHP: INSERT Into Database Using MySQL [duplicate]

This question already has answers here:
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 9 years ago.
I'm running this on my website:
$PlaceOrder = " Insert INTO Order VALUES ( '$CustomerID' , '$ItemID' , '$Quantity' , '$Date' ) ";
$result = mysql_query ($PlaceOrder);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
But when I ever I do I keep getting the following error message:
Invalid query: 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 VALUES ( '3' , '1' , '12' , '11/05/2013' )' at line 1
I really have no idea what to do now, I tried specifying the columns but that didn't work either. I am very new to this whole thing and I'm executing this based on whats in the manual.
Let's look at your error message again:
[...] the right syntax to use near'OrderVALUES
The first thing it complains about is Order. That's what you substitute the Table from your question for. So let's assume that's your actual table name.
It's also a reserved word. Enclose reserved words in backticks when used as column or table name identifiers.
Order is Key word Just use another word instead of Order
Are you sure there are only 4 columns and are they in the same order as you have specified in your query?
As you suggested try to add the column names, for example:
$PlaceOrder = "INSERT INTO Table (customer, item, quantity, date) VALUES ('$CustomerID', '$ItemID', '$Quantity', '$Date')";
Does this help you?
First of all, you should always specify the columns since you never know when you will have to add new columns to the table and old queries will start doing messy things if you haven't done that.
Having said that, are you sure those are the correct column data types that match the table's?
Also, make sure the date format is valid, you shoud use Y-m-d. You will find more info here.

error when try to update table name "order" [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 4 years ago.
i try to update simple data to table name "order" but i still get error.
i try to many version query but still same ;
first try :
$result = mysql_query("UPDATE order SET order_status_id=200 WHERE order_id=75") or die(mysql_error());
second try :
$result = mysql_query("UPDATE order SET order_status_id='200' WHERE order_id='75'") or die(mysql_error());
error ;
first try :
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 SET order_id=200 WHERE order_id=75' at line 1
second try :
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 SET order_status_id='200' WHERE order_id='75'' at line 1
Table structure
order_id int(11)
order_status_id int(11)
i try to update others table just to make sure my query correct and all table can update.
*Im using Opencart and my site use https.
Thanks.
order is a reserved word in MySQL. You need to escape it with backticks:
UPDATE `order` SET order_status_id=200 WHERE order_id=75
See MySQL reserved words

Categories