Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO prepared statement, correctly used?

Tags:

php

pdo

I just to need make sure I've got the PDO prepare statements correctly, will the following code be secured by SQL Injection?

$data['username'] = $username;
$data['password'] = $password;
$data['salt'] = $this->generate_salt();
$data['email'] = $email;

$sth = $this->db->prepare("INSERT INTO `user` (username, password, salt, email, created) VALUES (:username, :password, :salt, :email, NOW())");  
$sth->execute($data);
like image 980
John Avatar asked May 06 '12 16:05

John


1 Answers

Yes, your code is safe. It can be shortened however:

$data = array( $username, $password, $this->generate_salt(), $email );

// If you don't want to do anything with the returned value:
$this->db->prepare("
    INSERT INTO `user` (username, password, salt, email, created)
    VALUES (?, ?, ?, ?, NOW())
")->execute($data);
like image 64
Florian Margaine Avatar answered Sep 19 '22 02:09

Florian Margaine