I am curerntly trying to learn PDO use for my database connections as I have learnt this is the most "future proof". However I am having an issue in getting my minimum working example to work. Open submission I get two errors of 42000. The reported errors are on the same line ($stmt = $db->prepare("INSERT INTO table_test ('fieldsql') VALUES (:field)");), but I have tried different variations of what I thought it could be on this line and no luck. I have removed my username and password details.
With thanks!
<html>
<body>
<form method="post">
<table class="dd" width="100%" id="data">
<td>Field</td>
<td>:</td>
<td width="17%"><input type='textarea' name='field'/></td>
</table>
<input name='submit' type='submit' value='submit'/>
</form>
<?php
$db = new PDO('mysql:dbname=table_name;host=localhost', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
if(isset($_POST['submit']))
{
$field = $_POST['field'];
$stmt = $db->prepare("INSERT INTO table_test ('fieldsql') VALUES (:field)");
$stmt->execute( array('fieldsql' => $field) );
}
$link = null;
?>
</body>
</html>
For completeness the errors are: Fatal error: Uncaught exception 'PDOException' with message '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 ''fieldsql') VALUES (?)' at line 1' in C:\wamp\www\sqlinject.php on line 20
PDOException: 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 ''fieldsql') VALUES (?)' at line 1 in C:\wamp\www\sqlinject.php on line 20