gpt4 book ai didi

php - PDO MySQL 准备 INSERT 语法错误

转载 作者:行者123 更新时间:2023-11-29 01:19:49 25 4
gpt4 key购买 nike

已经看到大量类似的问题,但仍然无法找出发生了什么。

我正在使用 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());
}

已尝试使用 [] 转义所有内容并在表名之前指定数据库名称,但继续获取

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

最佳答案

INSERT INTO $date

$date var中好像有一个2017-08-11

如果要向'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());
}

关于php - PDO MySQL 准备 INSERT 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45536420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com