PDO Error 42000 on Insert Statement [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 8 years ago.
Hi I have the following with the below code? I have another statement which works but it is to another table and without the project_id column.
(
[0] => 42000
[1] => 1064
[2] => 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 'change (title, description, project_id) VALUES ('Test1', 'Test1', '1')' at line 1
)
$sql = "INSERT INTO change (title, description, project_id) VALUES (:title, :description, :project_id)";
$query = $db->prepare($sql);
$query->execute(array(":title" => $title,
":description" => $description,
":project_id" => $row_id));
$arr = $query->errorInfo();
print_r($arr);
Where have I gone wrong?

Always encapsulate your table and field names in backticks:
INSERT INTO `change` (`title`, `description`, `project_id`) VALUES (:title, :description, :project_id)
In this case: CHANGE is a keyword in MySQL, so your statement doesn't interpret it as a table name.

When you make tables always check here for reserved keywords in mysql
Your table name is a reserved keyword which is why you have these errors.
Use backticks to fix the problem. But i will recommend that you change the table name.
Sometimes prople find it hard to locate the backticks, it's above your tab key if you are using a normal qwerty or azerty keyboard.

Related

SQL Query not working - No reason? [duplicate]

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I have a problem. I created a SQL Query, to insert something into the database:
INSERT INTO order (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending)
VALUES ('232784', 'Niklas Peters', 'Some Stret', 'PostalCode', 'Wien', 'AT', 'email#email.com', '1454593142', '1')
But I always get 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 'order
(kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending)
' at line 1
My Code is PHP
$sql = "INSERT INTO order (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending) VALUES ('".$kdnr."', '".$emp."', '".$adresse."', '".$plz."', '".$ort."', '".$land."', '".$email."', '".$time."', '1')";
I just dont know what is wrong - Am I blind?
Cheers - would be glad for help!
Quote order with backticks:
INSERT INTO `order` (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending)
VALUES ('232784', 'Niklas Peters', 'Some Stret', 'PostalCode', 'Wien', 'AT', 'email#email.com', '1454593142', '1');
ORDER (R) is reserved word.
Reserved words are permitted as identifiers if you quote them
Your table name matches a reserved word, so will need to be quoted in SQL queries, e.g
INSERT INTO `order` (kundennummer,empfaenger,adresse,plz,ort,land,email,time,approvalPending) VALUES ('232784', 'Niklas Peters', 'Some Stret', 'PostalCode', 'Wien', 'AT', 'email#email.com', '1454593142', '1')
NB. When calling this from PHP, you may want to consider using prepared queries a la PDO, etc.

PHP $mysqli->prepare error with strange syntax when table name is "check" [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 8 years ago.
this call fails with error :
mysqli_report(MYSQLI_REPORT_ALL);
$stmt = $mysqli->prepare("INSERT INTO check VALUES (?,?,?,?,?,?)");
error i get :
Uncaught exception 'mysqli_sql_exception' with 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 'check VALUES
(?,?,?,?,?,?)' at line 1'
I have a table named "check" with right amount of fields
if i change table name to checkSomething it works ...
any idea ?
check is a reserved keyword. To use it as table name, you have to escape it with backticks like this: `check` :
$stmt = $mysqli->prepare("INSERT INTO `check` VALUES (?,?,?,?,?,?)");
Check is a reserved word in MySQL. You need to either surround it in backticks like this:
$mysqli->prepare("INSERT INTO `check` VALUES (?,?,?,?,?,?)");
Or much better, rename it to something that you don't need to constantly have a special case for.
$mysqli->prepare("INSERT INTO checks VALUES (?,?,?,?,?,?)");

Error with MySql query [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 8 years ago.
I am having problems with this MySql query:
INSERT INTO groups (NAME, DESC, TIME, OWNER) VALUES ('$GNAME', '$DESC', '$TIME', '$UID')
Essentially, the script adds the Group Name, Decription, Time and the Username of the person who registered the Group, into the mysql database.
Full script:
<?php
include_once('include/session.php');
$GNAME = $_POST['groupname'];
$DESC = $_POST['desc'];
$SPAM = $_POST['spam'];
$UID = $_POST['UID'];
$TIME = date('Y-m-d H:i:s');
if($SPAM == "queuee"){
$query ="INSERT INTO groups (NAME, DESC, TIME, OWNER) VALUES ('$GNAME','$DESC','$TIME', '$UID')";
$result = mysql_query($query) or die("There as been an Error! <hr>Error:<hr>".mysql_error() ."<br><hr>Go Back");
header("Location: ../group.php?id=$GNAME");
}else{
?>
The Security Question was wrong. Try Again.
<?
}
?>
I ran the Query directly into MySql itself using Phpmyadmin. However it still threw an error.
The error is:
#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, TIME, OWNER) VALUES ('$GNAME', '$DESC', '$TIME', '$UID')' at line 1
Any help at all is much appreciated and perhaps I am being a complete fool and not noticing an obvious mistake. If you need any more information just ask!
Many Thanks.
"DESC" is a keyword. Put it in backquotes.
..., `DESC`, ...
DESC is a reserved keyword. If you must use it for a column name, wrap it in backticks:
`DESC`
Try:
INSERT INTO groups (`NAME`, `DESC`, `TIME`, `OWNER`) VALUES ('$GNAME','$DESC','$TIME', '$UID')";
You're using reserved words as column names - PHP gets terribly confused in that case.
I should also add that your code is open to SQL injection, and you should look at moving away from using the mysql_* functions.
The quickstart guide for mysqli is at http://www.php.net/manual/en/mysqli.quickstart.php
PDO is another option; the information on prepared statements is at http://www.php.net/manual/en/pdo.prepared-statements.php
DESC is SQL keyword, use `` to escape column names:
$query ="INSERT INTO `groups` (`NAME`, `DESC`, `TIME`, `OWNER`) VALUES ('$GNAME','$DESC','$TIME', '$UID')";
According to MySQL Reserved Words, the word `DESC can't be used as a field name, unless you enclose it with backticks.
Put single (or double maybe) quotes around DESC in the "INSERT INTO groups" part. DESC is a reserved word and must be quoted.
use
$DESCRIPTION = $_POST['desc'];
inseted of
$DESC = $_POST['desc'];
$DESCRIPTION variable use in you query
$query ="INSERT INTO groups (NAME, DESC, TIME, OWNER) VALUES ('$GNAME','$DESCRIPTION','$TIME', '$UID')";

Naming MySQL table column

I am encountering one little problem here:
I am entering one MySQL query through PHP. I have checked the connection works fine and looks like the following:
INSERT INTO table (q1,q2,q3) VALUES ('".$_POST['Q1']."', '".$_POST['Q2']."'...)
then when I change the query to the following, there is an error:
INSERT INTO table (q1,q2,q3-1) VALUES ('".$_POST['Q1']."', '".$_POST['Q2']."'...)
The following error appears:
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 '-1) VALUES ('N', 'asdfasdf', '' )' at line 1
Now, I am thinking, is it because:
I name the table column as 'q3-1'
or any other problem?
Would it be okay if I change it to q3_1 instead?
If you put backticks ` around the field names, it should be OK
ie:
INSERT INTO table (`q1`,`q2`,`q3-1`) ...
Use backticks. Look into PDO.
INSERT INTO table (q1,q2,`q3-1`)
VALUES ('".$_POST['Q1']."', '".$_POST['Q2']."'...)
You have error in your second query because of q3-1. It should be in quotes :
INSERT INTO table (q1,q2,`q3-1`) VALUES ('".$_POST['Q1']."', '".$_POST['Q2']."'...)
escape the columns name with backtick ( ` )
INSERT INTO table (`q1`, `q2`, `q3-1`)
VALUES ('".$_POST['Q1']."', '".$_POST['Q2']."'...)
but this statement is vulnerable with SQL Injection. Try using it with PDO
ex.)
<?php
$stmt = $dbh->prepare("INSERT INTO table (`q1`, `q2`, `q3-1`) VALUES (?, ?, ?)");
$stmt->bindParam(1, $_POST['Q1']);
$stmt->bindParam(2, $_POST['Q2']);
$stmt->bindParam(3, $_POST['Q3']);
$stmt->execute();
?>

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