Insert new records to a MySQL database using PDO in PHP

August 04, 2013 | 808 views | Comments

Prepare records to be inserted to a MySQL database using the PDO prepare() function in PHP, then use the execute() function to perform the query.

For example.

Add a new record using the named (:name) parameter marker.

$value = $_GET['value'];
$sth = $dbh->prepare("INSERT INTO TABLE (value) VALUES (:value)");
$sth->execute(array(":value"=>$value));

Add a new record using the question mark (?) parameter marker.

$value = $_GET['value'];
$sth = $dbh->prepare("INSERT INTO TABLE (value) VALUES (?)");
$sth->execute(array($value));