Checking for duplicates before inserting

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Keanman
Forum Newbie
Posts: 18
Joined: Fri Jun 13, 2003 3:19 pm
Contact:

Checking for duplicates before inserting

Post by Keanman »

Hey guys, I was just wondering the best way to check for duplicate files before inserting. Here is my current code (Which is not very helpful):

<?
include("header.inc");
include("menu.inc");
include ("details.inc");

//Post all values into variables from the enterForm.php form
$serial = $_POST['serial'];
$asset = $_POST['asset'];
$name = $_POST['name'];
$location = $_POST['location'];
$po = $_POST['po'];
$doe = date("F j, Y");
$warranty = $_POST['warranty'];

//Check to see if the user left the item name blank
if($name!="") {

//If fields are left blank, fill them with hyphons
if($serial=="") {
$serial="-";
}
if($asset=="") {
$asset="-";
}
if($description=="") {
$description="-";
}
if($warranty=="") {
$warranty="-";
}

if ($serials==$serial) {
if ($assets==$asset) {
printf("The serial number entered already exists");
}
}

if ($rs['serialNum']!=$serial) {
//Insert the data into the database and display to the user that this has been done
$query = "INSERT INTO assets SET serialNum = '$serial',assetNum = '$asset',itemName = '$name',location = '$location',po = '$po',entryDate = '$doe',warranty = '$warranty'";
mysql_query($query);
}
?>

<TR ALIGN="CENTER">
<TD>The data has been entered successfully</TD>
</TR>
<?
}

//If the user left the item name blank, infor the user it is required to complete this field
Elseif($name=="") {
?>

<TR ALIGN="CENTER">
<TD>You must enter a name and location</TD>
</TR>

<?
}

include("footer.inc");
?>


As you can see I have my own little way of checking in there which doesn't work. Can I make this work or should it be totally different? Any help is greatly appreciated.
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

Try this:
$query = "select count(*) from assets where serialNum = '$serial'";
list ($cnt)=mysql_query($query);
if ($cnt>0){
echo "The serial number entered already exists";
}
Post Reply