PDO syntax error, SQLSTATE[42000] [duplicate] - mysql

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 8 years ago.
I'm getting the following error, and I cannot see the issue. Thanks for any help.
Fatal error: Uncaught exception 'PDOException' with message '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 'unique) VALUES ('XX.XXX.XX.X','03/12/2012','XX.XXX.XX.X - 03/12/2012')' at line 1' in /home/content/07/XXX/html/header.php:11 Stack trace: #0 /home/content/07/XXX/html/header.php(11): PDOStatement->execute(Array) #1 /home/content/07/XXX/html/index.php(15): include('/home/content/0...') #2 {main} thrown in /home/content/07/XXX/html/header.php on line 11
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("d/m/Y");
$unique = $ip." - ".$date;
$data = array($ip, $date, $unique, $date);
$STH1 = $DBH->prepare("INSERT INTO uniques (ip, date, unique) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE date = ?");
$STH1->execute($data);

wrap uniquer with backtick since it is a reserved keyword
eg
INSERT INTO uniques (ip, date, `unique`) ...
MySQL Reserved Keyword List

Related

Error Number: 1064 when select from database

when select images from a database below error is shown & after i delete record in the database its remain the same number inside (id)
this is the error
Error Number: 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 '16,19,1,2,1,2,1 )' at line 1
select * from images where id in(,16,19,1,2,1,2,1 )
Filename: controllers/Admin.php
Line Number: 1046
and this is select statment :
if (!empty($data->images)) {
$qry = $this->db->query("select * from images where id in($data-
>images )");
$res['results'] = $qry->result();
}
and now my database is empty and this error still remaining ?
just remove the leading comma. like this.
select * from images where id in( 16,19,1,2,1,2,1 )
there is a leading comma in $data->images

What is wrong with this MySQL UPDATE syntax? [duplicate]

This question already has answers here:
What is the difference between the backtick and the square bracket in SQL statements?
(2 answers)
Closed 7 years ago.
SET #currentID = (SELECT MIN(ExperimentID) FROM Jobs_t WHERE JobStatus = 'ToRun');
UPDATE [Jobs_t]
SET [JobStatus] = 'Pending'
WHERE ExperimentID = #currentID;
SELECT #currentID
Jobs_t is a table with two columns, ExperimentID and JobStatus. My SQL syntax is wrong on line 2, UPDATE [Jobs_t], but I can't seem to find out why.
I updated the syntax to:
SET #currentID = (SELECT MIN(`ExperimentID`) FROM `Jobs_t` WHERE `JobStatus` = 'ToRun');
UPDATE `Jobs_t`
SET `JobStatus` = 'Pending'
WHERE `ExperimentID` = #currentID;
SELECT #currentID
But it still fails on that second line.
The full error message is:
System.Data.Odbc.OdbcException: ERROR [42000] [MYSQL][ODBC 5.3(a) Driver][mysqld-5.7.9-log]
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 'UPDATE Jobs_t SET JobStatus = 'Pending' WHERE `ExperimentID = #currentID; S' at line 2
Trying this as an answer. Change
[Jobs_t]
to
`Jobs_t`
The character below the ~ on the top left of your keyboard. Change that for all instances of your brackets.

working perl ascript now says .DBD::mysql::db do failed: You have an error in your SQL syntax;

I got this perl script and it used to work fine till recently.
i am getting this error message.
DBD::mysql::db do 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 ' '')
ON DUPLICATE KEY UPDATE value=''' at line 2 at import_productfeatures.pl line 71.
DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL serve r version for the right syntax to use near ' '')
ON DUPLICATE KEY UPDATE value=''' at line 2 at import_productfeatures.pl line 71.
foreach my $feature (#features) {
my $cat_featureid = $feature->{CategoryFeature_ID};
my $value = $feature->{Presentation_Value};
my $sql = "INSERT INTO products_features (product_id, feature_id, value)
VALUES (".$prodid.", ".$cat_featureid.", ".$dbh->quote($value).")
ON DUPLICATE KEY UPDATE value=".$dbh->quote($value);
$dbh->do($sql);
}
You should use placeholders, instead of putting the values directly into the string:
my $sql = "INSERT INTO products_features (product_id, feature_id, value)
VALUES (?,?,?)
ON DUPLICATE KEY UPDATE value=?";
my $sth = $dbh->prepare($sql);
foreach my $feature (#features) {
my $cat_featureid = $feature->{CategoryFeature_ID};
my $value = $feature->{Presentation_Value};
$sth->execute($prodid,$cat_featureid,$value,$value);
}
$sth->finish();
DBI will handle the correct escaping for you.
Print out the value of $sql so you can see the SQL statement that you are building. Then you can see what the syntax problem is, or post it here so we can diagnose it.
However, even more than that, you should be using parametrized queries, not building SQL statements with untrusted external data. You are leaving yourself open to SQL injection. Please see http://bobby-tables.com/perl.html for examples on how to do it properly.
I think u missed single quote.
change
my $sql = "INSERT INTO products_features (product_id, feature_id, value)
VALUES (".$prodid.", ".$cat_featureid.", ".$dbh->quote($value).")
ON DUPLICATE KEY UPDATE value=".$dbh->quote($value);
to
my $sql = "INSERT INTO products_features (product_id, feature_id, value)
VALUES (".$prodid.", ".$cat_featureid.", '".$dbh->quote($value)."')
ON DUPLICATE KEY UPDATE value='".$dbh->quote($value."'");

SQL syntax error in query

What is wrong with my MySQL Query that is displaying 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 'index WHERE id=1' at line 1
Relevant code:
$qu = mysql_query("SELECT * FROM index WHERE id= 1") or die("MySQL ERROR: ".mysql_error());
WHILE($d = mysql_fetch_array($qu)):
$con = $d['content'];
endwhile;
index is a MySQL reserved keyword, so you must quote it with backticks like this:
SELECT *
FROM `index`
WHERE id = 1

mysql: Error Number: 1064

I'm trying to delete some data from 3 tables. Here is my SQL:
DELETE
FROM productdetail
JOIN (productdescription,productmineralcategories,productspeciescategories)
ON
(
productdetail.id = productspeciescategories.id_product
AND productdetail.id = productmineralcategories.id_product
AND productdetail.id = productdescription.id_product
)
WHERE productdetail.id='".$data['id'].
And here is the output error:
Error Number: 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 'JOIN (productdescription, productmineralcategories,
productspeciescategories) ' at line 3
What does it mean and how can I fix it?
Your DELETE statement should be:
$query="DELETE
FROM productdetail
WHERE productdetail.id='".$data[$id] . "'";
OR
$query="DELETE
FROM productdetail
WHERE productdetail.id='$data[$id]'";
OR do not add single quote if field type is numeric.
$query="DELETE
FROM productdetail
WHERE productdetail.id=$data[$id]";
Have a look at DELETE JOIN syntax.