On form submit, I’m getting a blank page (insert.php) with no error and no success message.
This is the form:
<form action="insert.php" method="post">
Firstname: <input type="text" name="first_name" id="first_name" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
This is the script:
mysql_select_db("my_db", $con);
$stmt = $db->prepare('INSERT INTO my_table (first_name) VALUES (:first_name)');
$stmt->execute(':first_name', $first_name);
if (!mysql_query($stmt,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
You need to create a PDO object to be able to use prepared statements. Instead you have opened a connection with
mysql_connect(). The two do not mix, and PDO is preferred between them as it is more easily secured through the use of prepared statements (among other reasons).From the PDO docs:
Pass an associative array to
execute(), rather than a list of arguments representing your placeholders. TheThe following call to
mysql_query()is unnecessary, as you have already executed the prepared statement with PDO.