I want to use PHP to check if a value already exists in the database and prevent that value from being entered twice. If the value does not exist, then we insert. I have tried with a PHP if statement, but could not get it right. I get the message:
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':email' at line 1
Here is my insert script:
try
{
$sql= 'INSERT INTO user_registration SET
email= :email,
date = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':email', $_POST['email']);
//$test=$s->execute();
}
catch (PDOException $e)
{
echo "Problem with script".$db_name.$e->getMessage();
}
try
{
$sql=$pdo->query("SELECT email FROM user_registration WHERE email = :email");
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':email', $_POST['email']);
$stmt->execute();
}
catch (PDOException $e)
{
echo "Problem exisyts".$e->getMessage();
}
if ($stmt->rowCount > 0) {
echo "Already exists";
exit();
}
else
{
$s->execute();
}