简体   繁体   中英

Inserting Into A Database With PDO Commands

I am learning PDO commands and have been following the examples

My query below works fine for the SELECT command but not for the INSERT command. I cannot see what is wrong with the INSERT command. The error I am getting is 'Uncaught Exception (PDOException) with message SQLSTATE[42000]: syntax error or access violation : 1064 You have an error in your SQL syntax'

$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;

错误可能是由列名周围的单引号引起的,请尝试使用:

$railinfo2 = $mysql_link->prepare('INSERT INTO berthmove (msg_type_berth) VALUES(:msg_type_berth)');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM