Page 1 of 1

Checking for duplicates before inserting

Posted: Tue Jul 15, 2003 7:24 am
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.

Posted: Tue Jul 29, 2003 8:34 pm
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";
}