PDA

View Full Version : duplicate entries in database


xashx
05-14-2010, 04:56 PM
I'm trying to prevent duplicate name entries from entering the MYSQL database. Need help. Here's my code.

<html>
<body>
<?php
$con = mysql_connect("localhost","nick","3950");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("registration form", $con);

$query = ("SELECT COUNT(*)
FROM `employees`
WHERE firstname= 'nick' and lastname= 'bouldin'");
$result= mysql_query($query);
$num= mysql_num_rows($result);

if ($num == 0)

{ $sql = "INSERT INTO employees (FirstName, LastName, Gender, Age)
VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[age]')";
}
else
{
Print "Firstname and lastname already exists";
}

mysql_close($con)
?>

</body>
</html>

davidj
05-15-2010, 11:53 AM
you need to do a SELECT on the table to see if the posted name exists

if it exists then you notify user

if it doesnt then just go ahead and add them to the database

xashx
05-17-2010, 05:46 AM
The first and last name is in the database but I still keep getting the "Firstname and lastname already exists" message. Is this code incorrect? Because it seems that this problem emanates from this code. $query = ("SELECT COUNT(*)
FROM `employees`
WHERE firstname= 'nick' and lastname= 'bouldin'");
$result= mysql_query($query);
$num= mysql_num_rows($result);

if ($num == 0)

davidj
05-18-2010, 11:47 AM
do you have the first name , last name as unique fields in the database