我正在学习PDO命令并且一直在关注示例
我的查询适用于SELECT命令,但不适用于INSERT命令。我看不出INSERT命令有什么问题。我得到的错误是'未捕获异常(PDOException),消息SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法中有错误'
$descr='1M18';
Require('connect_db.php');
//SELECT COMMAND
$stmt=$mysql_link->prepare("SELECT descr,area_id FROM berthmove WHERE descr=:descr");
$stmt->execute(array(':descr'=>$descr));
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
echo $row['descr'];
echo $row['area_id'];
}
//INSERT COMMAND
$msg_type_berth='Hello';
$railinfo2=$mysql_link->prepare("INSERT INTO berthmove ('msg_type_berth') VALUES(:msg_type_berth)");
$railinfo2->execute(array(':msg_type_berth'=>$msg_type_berth));
$mysql_link=null;
答案 0 :(得分:2)
可能错误是由列名称周围的单引号生成的,请尝试使用:
$railinfo2 = $mysql_link->prepare('INSERT INTO berthmove (msg_type_berth) VALUES(:msg_type_berth)');