I have a form that I'm using to post records to a mysql database table called activitytracking. What I want to be able to do is keep people from posting duplicate records for the the same employee ID and date of service. As a matter of fact, the two fields in the table that I want to use to do this are called ANumber, which is the employees personal ID number. And DateOfService, which is the actual date the employee engaged in a particular activity. Below is the code for the form. Forgive me for the amount of code I'm showing.
<?
include ("Header.inc.php");
include ("DBConnection.inc.php");
?>
<?
if($submit) {
if (!$ANumber) {
$error = "Sorry! You didn't fill in the employee's A Number!";
} else {
if (!$FirstName) {
$error = "Sorry! You didn't enter the employee's First Name!";
} else {
if (!$LastName) {
$error = "Sorry! You didn't enter the employee's Last Name!";
} else {
if (!$TeamLeader) {
$error = "What's the name of this employee's Team Leader?";
} else {
if (!$Department) {
$error = "What department does this employee work in?";
} else {
if (!$DateOfService) {
$error = "On what date did this employee engage in this activity?";
} else {
if (!$NumberOfHours) {
$error = "How many hours did this employee engage in this activity?";
} else {
if (!$ActivityType) {
$error = "What Activity did this employee engage in?";
} else {
if (!$Org) {
$error = "What is the name of the organization involved?";
} else {
if (!$Description) {
$error = "If you have no comments please enter N/A!";
} else {
if (!$TimeTakenWhen) {
$error = "Was this during work hours or after work hours?";
} else {
if (strlen($ANumber) < 7 || strlen($ANumber) > 8) {
$error = "The employee ID number must be no more or less than 7 charactures long. Be sure to include the alpha characture of the employee ID";
} else {
if (!is_numeric($NumberOfHours['var']) != false) {
$error = "You can only enter the number of hours in the hours field.";
} else {
$Query = "Insert activitytracking (ANumber, FirstName, LastName, TeamLeader, Department, DateOfService,
NumberOfHours, ActivityType, Org, Description, TimeTakenWhen, siteID)
Values ('$ANumber', '$FirstName', '$LastName', '$TeamLeader', '$Department',
'$DateOfService', '$NumberOfHours', '$ActivityType', '$Org',
'$Description', '$TimeTakenWhen', '$siteid')";
print "Thank you. Your record has been submitted to the database.";
//print $Query;
$Result = mysql_db_query ($DBName, $Query, $Link);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
if (!$submit || $error || $error2) {
echo $error;
?>
<body bgcolor="#eeeeee" text="#000000">
<form action=<? print $PHP_SELF ;?> Method=get Name=frm>
<input type=hidden name=siteid value=<?print $siteid;?>>
<Table border=0 align="center">
<tr>
<font size=4.5>
Community Service Activity Tracking
</tr>
<tr>
<td><b>
ANumber
</td>
<td>
<Input type=text name=ANumber value="<?php echo $ANumber ?>">
</td>
</tr>
<tr>
<td><b>
First Name
</td>
<td><b>
<Input type=text name=FirstName value="<?php echo $FirstName ?>">
</td>
</tr>
<tr>
<td><b>
Last Name
</td>
<td>
<Input type=text name=LastName value="<?php echo $LastName ?>">
</td>
</tr>
<tr>
<td><b>
Team Leader
</td>
<td>
<Input type=text name=TeamLeader value="<?php echo $TeamLeader ?>">
</td>
</tr>
<tr>
<td><b>
Department
</td>
<td>
<?
$db = mysql_select_db("$DBName",$Link);
echo" <select name=\"Department\"> "; //start the select box
$results= mysql_query("SELECT ID, Name from testtable Order by Name asc");
$id = "ID";
$idname = "Name";
echo mysql_error();
if (mysql_Numrows($results)>0) //if there are records in the fields
{
$numrows=mysql_NumRows($results); //count them
$x=0;
while ($x<$numrows){ //loop through the records
$theId=mysql_result($results,$x,$id); //place each record in the variable everytime we loop
$theName=mysql_result($results,$x,$idname);
echo "<option value=\"$theName\">$theName</option>\n"; //and place it in the select
$x++;
}
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td><b>
Date Of Activity
</td>
<td>
<Script Language="JavaScript">
function showCalendar(frm,textbox){
window.open("Calendar.php?frm=" + frm + "&txt=" + textbox,"DatePicker","width=250,height=230,status=no,resizable=no,top=200,left=200");
}
function getObject(obj){
alert(obj.value);
}
</Script>
<input type=text name=DateOfService value="<?php echo $DateOfService ?>">
<img src="kalendar.gif" width="28" height="24" style="cursor:hand" onclick="showCalendar('frm','DateOfService')">
</td>
</tr>
<tr>
<td><b>
Hours
</td>
<td>
<Input type=text name=NumberOfHours value="<?php echo $NumberOfHours ?>">
</td>
</tr>
<tr>
<td><b>
Activity
</td>
<td>
<?
$db = mysql_select_db("$DBName",$Link);
echo" <select name=\"ActivityType\"> "; //start the select box
$results= mysql_query("SELECT ID, Names from activitytypes Order by Names asc");
$id = "ID";
$idname = "Names";
echo mysql_error();
if (mysql_Numrows($results)>0) //if there are records in the fields
{
$numrows=mysql_NumRows($results); //count them
$x=0;
while ($x<$numrows){ //loop through the records
$theId=mysql_result($results,$x,$id); //place each record in the variable everytime we loop
$theName=mysql_result($results,$x,$idname);
echo "<option value=\"$theName\">$theName</option>\n"; //and place it in the select
$x++;
}
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td><b>
Organization
</td>
<td>
<Input type=text name=Org value="<?php echo $Org ?>">
</td>
</tr>
<tr>
<td><b>
Comments
</td>
<td>
<TextArea NAME="Description" ROWS="7" COLS="62" style="font-family: Arial"><?php echo $Description?></TextArea>
</td>
</tr>
<tr>
<td><b>
Taken When
</td>
<td>
<?
$db = mysql_select_db("$DBName",$Link);
echo" <select name=\"TimeTakenWhen\"> "; //start the select box
$results= mysql_query("SELECT WhenID, WhenInvolved from WhenWorked Order by WhenInvolved asc");
$id = "WhenID";
$idname = "WhenInvolved";
echo mysql_error();
if (mysql_Numrows($results)>0) //if there are records in the fields
{
$numrows=mysql_NumRows($results); //count them
$x=0;
while ($x<$numrows){ //loop through the records
$theId=mysql_result($results,$x,$id); //place each record in the variable everytime we loop
$theName=mysql_result($results,$x,$idname);
echo "<option value=\"$theName\">$theName</option>\n"; //and place it in the select
$x++;
}
}
echo "</select>";
?>
</td>
</tr>
</Table>
<Table align="center">
<Input type=submit align="center" name=submit value="Post">
</Table>
</form>
<?php
}
//end if
?>
[/b]