0
votes

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;
1
look at your post. Look at the masses of whitespace in your code. Don't just copy-paste from your editor, always preview your post to make sure it looks right before hitting submit. (especially note that the code block is based on 4 spaces. Don't add more leading space or you're just creating a huge block of unnecessary scrolling) - Mike 'Pomax' Kamermans
Did you try to connect to MySQL with the CLI tool (> mysql -u... -p... -h... yourdbname), and tried copy-pasting your query there to see what it says? - Mike 'Pomax' Kamermans
When I try pasting the problem line in it says ERROR 1064 (42000) : You have an error in your SQL syntax - user2635961

1 Answers

2
votes

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)');