NoS*_*ock 2 php mysql sql pdo syntax-error
已经看到了大量类似的问题,但仍然无法找出正在发生的事情.
我正在使用PHP的PDO来准备这样的语句:
try{
$statement = $db->prepare("INSERT INTO $date (name, surname, email, phone, comment) VALUES (:name, :surname, :email, :phone, :comment)");
$statement->bindParam(':name', $name);
$statement->bindParam(':surname', $surname);
$statement->bindParam(':email', $email);
$statement->bindParam(':phone', $phone);
$statement->bindParam(':comment', $comment);
$statement->execute();
}
catch(PDOException $e){
die("Connection to database failed: " . $e->getMessage());
}
Run Code Online (Sandbox Code Playgroud)
尝试使用[]转义所有内容并在表名前指定数据库名称,但继续获取
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 '2017-08-11 (name, surname, email,
phone, comment) VALUES ('Test', 'Test', 'Test@' at line 1
Run Code Online (Sandbox Code Playgroud)
INSERT INTO $ date
似乎有一个2017-08-11 in $ date var.
如果要将数据插入"2017-08-11"表,则应使用`符号进行转义
try{
$statement = $db->prepare("INSERT INTO `$date` (name, surname, email, phone, comment) VALUES (:name, :surname, :email, :phone, :comment)");
$statement->bindParam(':name', $name);
$statement->bindParam(':surname', $surname);
$statement->bindParam(':email', $email);
$statement->bindParam(':phone', $phone);
$statement->bindParam(':comment', $comment);
$statement->execute();
}
catch(PDOException $e){
die("Connection to database failed: " . $e->getMessage());
}
Run Code Online (Sandbox Code Playgroud)