Reputation: 17
I've creating a user management system. I want the php script to verify if email addresses exist already in the database. I can update user data. I can verify correct email format. The part of my script that isn't working is the part that checks if the email is already in use. Here is the part that doesn't work:
if($_POST['email'] !=$_POST['email'])
{
$query_email = "
SELECT
from users
where
email = :email
";
$query_goes = array(
':email' => $_POST['email']
);
try
{
$stmt = $db->prepare($query_email);
$result = $stmt->execute($query_goes);
}
catch (PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$row = $stmt->fetch();
if($row)
{
die("Email already in use...");
}
Error it throws: Ouch, failed to run query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicata du champ '[email protected]' pour la clef 'email'
Upvotes: 1
Views: 93
Reputation: 5371
You need to select something. You can do SELECT 1
since you're just checking whether the row exists or not and could care less to have physical data returned.
There will be no result if the email isn't there, there will be a result if there is.
$query_email = "
SELECT
1
from users
where
email = :email
";
On another note, how would this ever execute:
if($_POST['email'] !=$_POST['email'])
In what case would a variable not be equal to itself?
I can't picture this logic even running to begin with. Your error is for a duplicate entry, which has nothing to do with any code you've provided. My guess is that it's skipping this code and you're going straight to your insert.
Maybe if(isset($_POST['email']))
is what you want?
Upvotes: 1
Reputation: 192
Like juergen d said in comment, your query is wrong. Since you only want to check if the email exist I suggest to get the email column instead of all.
$query_email = "
SELECT <----- should be SELECT email
from users
where
email = :email";
Like this
$query_email = "
SELECT email
from users
where
email = :email";
Upvotes: 0