As with inserting new records, updating records via the PHP script is simply a case of using PDO::query() .if a user is passing literal values in the UPDATE statement, or PDO::prepare() with placeholders. For example, the following script changes the email address field in the “ Aman nagpal ” record.
<?php
$dsn = “mysql:dbname=mydatabase”;
$username = “root”;
$password = “mypass”;
try {
$conn = new PDO( $dsn, $username, $password );
$conn- > setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo “Connection failed: “ . $e- > getMessage();
}
$id = 8;
$newEmailAddress = “[email protected]”;
$sql = “UPDATE members SET emailAddress = :emailAddress WHERE id = :id”;
try {
$st = $conn- > prepare( $sql );
$st- > bindValue( “:id”, $id, PDO::PARAM_INT );
$st- > bindValue( “:emailAddress”, $newEmailAddress, PDO::PARAM_STR );
$st- > execute();
} catch ( PDOException $e ) {
echo “Query failed: “ . $e- > getMessage();
}
?>