I’m a pdo newbie and need to prepare and execute some php/pdo code:
This code works to allow me to create users in my database:
// Perform Insert / Update
$STH = $dbh->prepare("INSERT INTO users (username, email) values (:username, :email)");
$STH->bindParam(':username', $username);
$STH->bindParam(':email', $email);
try{
$STH->execute();
redirect_to(signupsuccess.php);
}
catch(PDOException $e) {
echo "I'm sorry, Dave. I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
}
but before I put that data into the database I need to do a check to make sure the email doesn’t already exist in the database. I need to do a SELECT, something like:
$STH = $dbh->prepare("SELECT FROM users (email) values (:email)");
$STH->bindParam(':email', $email);
try{
$STH->execute();
}
and I know I need to add
"WHERE something matches '$_POST'email') something.." ...
I’m totally lost at this point..I can do this without PDO but I want to start using PDO’s prepared statements..Please help!
Just do a simple select. I recommend Using bind value if you may possibly have a null email.
Then just check if any records are returned. If so, Update, don’t insert. good luck.