Am not experienced, so appreciate any help.
Am looking to build a PHP to confirm that registration of user has a unique email address. If unique, then to add to sql database, and echo out respective value.
This is what I have – the echo value is produced if the email address is duplicate, but the echo value is not produced and the db not updated if it’s unique.
<?php
$email= $_GET["email"];
$firstname = $_GET["firstname"];
$lastname = $_GET["lastname"];
$con = mysql_connect("user","db","pass");
mysql_select_db("db");
$query1 = "SELECT * FROM `db` WHERE `Email` = '$email'";
$result = mysql_query($query1);
if ( mysql_num_rows ($result) == 1 )
{$value="Already registered";
}
else
{
mysql_query("INSERT INTO db (Email, FirstName, LastName) VALUES ('$email','$firstname','$lastname')");
mysql_close($con);
$value="Confirmed";
}
echo $value;
Instead of getting all rows, take only one of them like this. It will save lots of memory.
Then the following line
will be logically correct. Otherwise if there are 2 or more records in the database, the result is false. With my fix this
ifwill be logical.The other solution would be to select the number of the rows, but since the emails are to be unique in the database, there is no point doing something like
However this way, instead of checking the amount of rows in the result, you can check the result without fearing it being empty. But again, since your database’s logic is to have one email only once, there is no point checking how many of them there are since the result will be either 0 or 1 🙂