user2635961
user2635961

Reputation: 379

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;

Upvotes: 0

Views: 56

Answers (1)

Marcio Mazzucato
Marcio Mazzucato

Reputation: 9305

Probably the error is generated by the single quotes around the column name, try to use:

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

Upvotes: 2

Related Questions