Hi,
I'm making the move from using MySQLi to PDO and thought I best double check what I'm doing is secure and efficient code? I've got a contact form which accepts user input, then sends the email to a specific email address also writing the email content to a MySQL database. Below is just the connection and insert part of my contact form.
db-config.php
$config = array(
'host' => 'localhost',
'username' => 'myUser',
'password' => 'myPass',
'dbname' => 'myDatabase'
);
contact-form.php
include ('db-config.php');
try {
$dbh = new PDO('mysql:host='. $config['host'] .';dbname='. $config['dbname'], $config['username'], $config['password']);
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("INSERT INTO myForm(name, email, email_msg) VALUES (:name, :email, :email_msg)");
$insert = array(
':name' => $name,
':email' => $email,
':email_msg' => $email_msg
);
$sth->execute($insert);
With MySQLi you have to run user input through mysqli_real_escape_string, however am I right in thinking by me using a prepared statement and using :name, :email and :email_msg this is secure? Therefore would I not need to do the following sanitization on user input before inserting into the database?
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
Thanks in advance.