mysql query work on phpmyadmin but not work on mysqli - php

I have a Query That work in phpmyadmin but not work on php (mysqli)
where is the problem ?
Query:
INSERT INTO `SepidarSoft_Portal`.`Archive_Media` SET `CTime`='1364135670',`UTime`='1364135670',`PID`='',`State`='1',`Sequence`='0',`Subject`='Hojom Marg ( www.Parstafrih.ir )',`Text`='',`Description`='',`Definition`='',`KeyWord`='',`ETag`='',`Access`='',`LinkToPage`='',`Attachment`='[{\"Name\":null,\"Kind\":null,\"Size\":false,\"Address\":\"27\",\"More\":{\"Original\":1}}]',`STime`='0',`ETime`='0';
SET #LAST_ID:=LAST_INSERT_ID();
INSERT INTO `SepidarSoft_Portal`.`Archive_Media_MoreInfo` (`id`,`Key`,`Value`) VALUES (#LAST_ID,'Instrumental','1'),(#LAST_ID,'KindFile','صوتی'),(#LAST_ID,'Genre','نغمه'),(#LAST_ID,'SName','Amir Tajik ( www.Parstafrih.ir )'),(#LAST_ID,'Events','[[\"\"]]'),(#LAST_ID,'Album','( www.Parstafrih.ir )'),(#LAST_ID,'Composer',''),(#LAST_ID,'Adjustment',''),(#LAST_ID,'Subtitle','[object HTMLInputElement]'),(#LAST_ID,'Release','');
Error:
#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 'SET #LAST_ID:=LAST_INSERT_ID();
1) I use php mysqli_multi_query for this

Your issue is simple.
Backticks ( ` ) are used to execute commands in php. That's why you're being given a syntax error. Replace them with single or double quotes within your mysqli functions.
Kindly read the following document and you should be sorted :)
Backticks on php.net

Related

#1064 - You have an error in your SQL syntax;heck the manual that corresponds to your MySQL server version for the right syntax to use near

the below is my query and when i run in db its shows error like #1064 - You have an error in your SQL syntax;
INSERT INTO`expense`(Request,Date,Employee,Load,Truck,Amount,Purpose,Mode,Remark)
VALUES ('$Request','$Date','$Employee','$Load','$Truck','$Amount','$Purpose','$Mode','$Remark')
Can sumbdy help!!
Date & Load are the reserved keywords. Use back ticks -
INSERT INTO `expense` (`Request`,`Date`,`Employee`,`Load`,`Truck`,`Amount`,`Purpose`,`Mode`,`Remark`)
VALUES ('$Request','$Date','$Employee','$Load','$Truck','$Amount','$Purpose','$Mode','$Remark')
use mysqli_real_escape_string might be some problem with variable containing string's
$Request = mysqli_real_escape_string($con, $Request);
INSERT INTO `expense`(`Request`,`Date`,`Employee`,`Load`,`Truck`,`Amount`,`Purpose`,`Mode`,`Remark`)
VALUES ('".$Request."',............)

Php mysqli->real_escape_string and MYSQL

I've a little question.
I've written this code to add the values to mysql database but when i run the code and I got an error. Can anybody help me?
the code:
$fel = $mysqli->query("INSERT INTO deleted (uid,buy_type,prop_type,district,street,room_min,room_max,price_min,price_max,condition_type,heat_type,lift_type,parking_type,type_of_del,when)
VALUES ('".$mysqli->real_escape_string($letomb['uid'])."',
'".$mysqli->real_escape_string($letomb['buy_type'])."',
'".$mysqli->real_escape_string($letomb['prop_type'])."',
'".$mysqli->real_escape_string($letomb['district'])."',
'".$mysqli->real_escape_string($letomb['street'])."',
'".$mysqli->real_escape_string($letomb['room_min'])."',
'".$mysqli->real_escape_string($letomb['room_max'])."',
'".$mysqli->real_escape_string($letomb['price_min'])."',
'".$mysqli->real_escape_string($letomb['price_max'])."',
'".$mysqli->real_escape_string($letomb['condition_type'])."',
'".$mysqli->real_escape_string($letomb['heat_type'])."',
'".$mysqli->real_escape_string($letomb['lift_type'])."',
'".$mysqli->real_escape_string($letomb['parking_type']).",
'".$mysqli->real_escape_string($type_of_del)."',
now())") or die($mysqli->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 'when) VALUES ('3', 'kiado', 'lakas', '1'' at line 1
WHEN is a reserved word. Enclosing it in backticks should fix your problem, as it will then be treated as an identifier.
`when`
You should use backticks around your column names. when is a MySQL keyword so it's being interpreted incorrectly. At the very least use backticks around when.

Mysql Syntax Error (I cant find what the error is!)

I have the following mysql query:
REPLACE INTO application (export_date,application_id,title,recommended_age,artist_name,seller_name,company_url,support_url) VALUES (1362564068339,564783832,Eyelashes,4+,Char Room,Char Room,http://,http://ios.charroom.net/,http://itunes.apple.com/app/)
I get 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 'Char Room,Char Room,http://,http://ios.charroom.net/,http://itunes.apple.com/app' at line 1
I cant seem to see where the error is. Can someone help me out?
You must know that the above is an echo of the actual query. All the parameters in this query went through mysql_real_escape_string before being to the query.
Well, firstly you shouldn't be using mysql_real_escape_string:
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
Secondly, you still need to put the quotes around the strings. mysql_real_escape_string will escape quotes within the string, but it doesn't add the quotes to the start and end.
You have to put quotes around data you are inserting in your database.

Mysql Won't update the row [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Whats wrong with this query mysql?
I am using mysql and php to update a record. Here is my code:
$n=mysql_query("UPDATE chondas SET model='$model1', yearstart=$yearstart1,
yearstop=$yearstop1, desc='$desc1', hp='$hp1',
engine='$engine1',trim='$trim1', weight='$weight1' WHERE id=$id1");
In the following code if I take out desc='$desc1' everything works perfectly. What would cause this error?
When i tested the following code in phpmyadmin I got this error:
#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 'desc='text of the textarea' at line 1.
DESC is a reserved word in mysql so you need to use backticks:
UPDATE chondas SET model='$model1', yearstart=$yearstart1, yearstop=$yearstop1, `desc`='$desc1', hp='$hp1', engine='$engine1',trim='$trim1', weight='$weight1' WHERE id=$id1
You should also switch to PDO (or mysqli) and prepared statements with bound variables to avoid potential sql injection.
DESC is a reserved word in MySQL.
Escape it with backticks
`desc` = '$desc1'
Your query won't run if you include the variable names in phpMyAdmin, such as:
UPDATE chondas SET model='$model1', yearstart=$yearstart1,
yearstop=$yearstop1, **desc='$desc1'**, hp='$hp1',
engine='$engine1',trim='$trim1', weight='$weight1' WHERE id=$id1
Also, the PHP won't run if you mix your "" and '' e.g: "UPDATE chondas SET model='$model1'...

How to ignore special characters(" ' , - \ / etc) while data insert

I storing articles in database that contains special characters like ", ', etc. but it gives error while saving in MySQL:
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 's and Moral Science's books in school. I clearly remember the picture of a Hindu' at line 1
mysql_real_escape_string everything you put into a query. Always. No exceptions.
Alternatively, use prepared statements.
use mysql_real_escape_string

Categories