Hello everybody. Im a newbie in php and I really need your guys help and opinion on my problem. I have created a simple registration form in php and Im having problem of detecting existing user in database. For example, if I enter staff id number that already existed, I want it to detect and display an error message stating the person already existed. Below are my code:
Code:
<?php
include ('mysql_connect.php');
if (isset($_POST["Submit"]))
{
if (empty($_POST['staffid']))
{
echo "Please enter the staffid";
}
else
$staffid = $_POST['staffid'];
if (empty($_POST['name']))
{
echo "Please enter the name";
}
else
$name = $_POST['name'];
if (empty($_POST['address']))
{
echo "Please enter staff address";
}
else
$address = $_POST['address'];
if (empty($_POST['department']))
{
echo "Please enter the department";
}
else
$department = $_POST['department'];
if ($staffid && $name && $address && $department)
{
$query1 = "SELECT * FROM staff";
$result1 = @mysql_query($query1);
$row = mysql_fetch_array($result1);
if ($row['Staff_ID'] != $staffid)
{
$query = "INSERT INTO staff VALUES ('$name', '$address', '$staffid', '$department', now())";
$result = @mysql_query($query);
if ($result)
{
echo "$name information has been saved in database";
}
}
else
{
echo "The person of staff id $staffid has already existed";
}
}
}
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="75%" border="0">
<tr>
<td><font face="Arial, Helvetica, sans-serif">Staff ID</font></td>
<td><input type="text" name="staffid"></td>
</tr>
<tr>
<td><font face="Arial, Helvetica, sans-serif">Name</font></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><font face="Arial, Helvetica, sans-serif">Address</font></td>
<td><textarea name="address"></textarea></td>
</tr>
<tr>
<td><font face="Arial, Helvetica, sans-serif">Department</font></td>
<td><input type="text" name="department"></td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
Note: The column in the database are:
Name, Address, Staff_ID (primary key), Department, Date_reg