I'm using Node Mysql
var values = []; //array with actual values
var sql = "INSERT INTO table (field1, field2) VALUES ? ON DUPLICATE KEY UPDATE updated_at = now()";
connection.query(sql, values, function() {
});
And I get an 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
'ON DUPLICATE KEY UPDATE updated_at = now()
Meanwhile the same query works in mysql cli.
What could be the problem?
You are not placing any value on the "values" array, and the INSERT expects two. You can check how the SQL got built with:
var query = connection.query(sql, values, function() {});
console.log(query.sql);
Related
I'm trying to use the following code to update columns:
UPDATE user_profiles
SET range = '20', colResize = 'flex'
WHERE uid='472';
I get the following error:
Failed to execute SQL : SQL UPDATE user_profiles SET range = '20', colResize = 'flex' WHERE uid='472'; 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 'range = '20', colResize = 'flex' WHERE uid='472'' at line 1
What am I overlooking?
range is a reserved keyword in MySQL. Use backticks to escape the name or use another column name.
SET `range` = 20
I was trying to use the ON UPDATE DUPLICATE KEY clause for the first time, following this link
SQL - IF EXISTS UPDATE ELSE INSERT INTO
and I'm getting an error in my sql syntax:
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 ''AJAY KUMAR')'
at line 2 The SQL being executed was: INSERT INTO fee_acc_balance
(guardian_name, account_no, paid_amount, due, days_overdue,
total_fees, updated_on) VALUES ('AJAY KUMAR', '10', 0, 12550, 0,
12550, '2017-02-10 21:28:05') ON DUPLICATE KEY UPDATE guardian_name =
VALUES ('AJAY KUMAR') Error Info: Array ( [0] => 42000 [1] => 1064 [2]
=> You have an error..
The unique key in my case is account_no, and this is my sql :
INSERT INTO fee_acc_balance (guardian_name, account_no, paid_amount, due, days_overdue, total_fees, updated_on)
VALUES ('$father_name', '$account->account_no', $payments, $sum, 0, $sum,'$now')
ON DUPLICATE KEY UPDATE guardian_name = VALUES ('$father_name')
Where does the error lie?
You cannot specify an absolute value in ON DUPLICATE KEY UPDATE:
ON DUPLICATE KEY UPDATE guardian_name = VALUES ('$father_name')
Try with
ON DUPLICATE KEY UPDATE guardian_name = VALUES(guardian_name)
Notice that the right part of the assignment is the new field coming in from VALUES, and the left side is the extant record; "UPDATE a = VALUES(a)" means "put the a from VALUES into the record", not "leave everything as it is".
Also, you may want to write variables in curly brackets:
...VALUES ('{$father_name}', '{$account->account_no}', {$payments}, {$sum}, 0, {$sum}, '{$now}')
or even better use PREPAREd statement with PDO:
$stmt->prepare("INSERT... VALUES(?, ?, ?, ?, 0, ?, ?)");
$stmt->execute([
$father_name,
$account->account_no,
$payments,
$sum,
$sum,
$now
]);
and, better still, bound parameters.
Otherwise, strange things might happen if the guardian name is Ajay Al'Kumar (note the quote mark) or a string value is passed instead of an integer one.
I keep getting this syntax error in my MySQL code within a PHP file. I'm simply trying to increment/add to the value already in the table with this time variable. If anyone could help me out I'd greatly appreciate it.
PHP:
$sql = "UPDATE Aircraft
SET MaintenanceFlightTime = (MaintenanceFlightTime + $MaintenanceDuration),
WHERE AircraftID = $AircraftID";
Error:
UPDATE Aircraft SET MaintenanceFlightTime = (MaintenanceFlightTime + 00:10:00), WHERE AircraftID = 8
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 ':10:00), WHERE AircraftID = 8' at line 2
You cannot just add a string like '01:01:01' to a time column but you can use ADDTIME()
$MaintenanceDuration = '00:10:00';
$sql = "UPDATE Aircraft
SET MaintenanceFlightTime = ADDTIME(MaintenanceFlightTime, '$MaintenanceDuration')
WHERE AircraftID = $AircraftID";
String sql = ("insert into registration(pic) values(?) where email='"+Email+"' ");
i get error :error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where email='yyy#ymail.com'' at line 1
You have to use UPDATE query to pass it like
String sql = "UPDATE registration SET pic = ? WHERE email = '" + Email + "'";
Syntax for UPDATE query is
UPDATE table_name SET column_name = value;
Insert query format should be,
"insert into tablename (columnname) values(coulmnvalue)"
OR
"update registration set pic='' where email='"+Email+"'";
Yes. that is impossible.
Either you want:
insert into registration(pic) values(?)
Which will give you a new row;
Or you want an UPDATE:
UPDATE registration SET pic = ?
WHERE email = <EMAILYouWant>
Which will update an existing row where email = the record with the email you want to update the pic column.
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."'");