Forum Moderators: coopster
1. in MySQL set a unique index on the field phone_number:
ALTER TABLE users ADD UNIQUE (`phone_number`);.
2. then simply run the insert or update query. It should return false if someone tries to insert a duplicate entry.
if (!mysql_query("INSERT INTO users (phone_number)
VALUES ('" . mysql_real_escape_string($_POST['phone_number']) . "')")) {
if (strpos(mysql_error(), 'Duplicate')!== false) {
echo 'hey, dupe entry!';
} else {
echo 'some other error occured...';
}
} else {
echo 'OK, the insert went fine.';
}
If someone enters the phone numer 071 456789 and the next day 071456789 and the next day +31 71 456789 they would test as different (UNIQUE) phone numbers while they are actually the same. I'd apply RonPK's suggestion and store..
- country code
- area code
- subscriber number
..in different fields and force users not to use spaces or special characters in your application code.
However if the insert happens at the end of a long process checking for many user’s input
Then you may not perform at that scrip level an insert but only check for dupli.
This is what I did
$conn = db_connect();
$sql = "SELECT email FROM auth_whatever where username = '$username' ";
$result = mysql_query($sql);
$email_exists=$_POST['email'];
while ($row=mysql_fetch_array($result))
{
if($email_exists == $row['email'])
{
echo"<h3> $email_exists is registered, </h3><a href='let's do it again.php'><br>
<b>Please, Retry and Use an Unregistered and Valid Email Address</b></a><p>";
exit();
}
else
{ // email validation, regex etc….