Forum Moderators: coopster

Message Too Old, No Replies

Avoiding same entry with PHP

         

Shanee

11:45 am on Oct 3, 2006 (gmt 0)

10+ Year Member



any one knows about php code/funtion how to avoid same enrtires to mysql

for example i have three rows

Phone_Number
Name
Address

and the Script allow to avoid the same phone number which already exist by the user/admin

Thanks
Shanee

henry0

12:24 pm on Oct 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For example if you have a name or other data that you can pass as a var or a session you could check:

Do a query for existing phone numbers WHERE name=$name
and check if $new_phone == a phone number where a name = $name

RonPK

1:38 pm on Oct 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's what I would do:

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.';
}

rubenski

8:29 pm on Oct 3, 2006 (gmt 0)

10+ Year Member



RonPK's suggestion would work, but remember to strictly define an input format for telephone numbers.

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.

henry0

9:45 pm on Oct 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I indeed agree RonPK ‘s solution is an excellent one.

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….