Help with my php form?

I have created a form using a php where it registers a new customer and connecting it to my uni database.
Now my problem is when i enter a customer id that is already registered it should say an error saying “Customer ID already exits” but what it does it gives me that error and also a statement saying “Customer has now been registered” . How do i fix this
[php]

<?xml version="1.0" encoding="utf-8"?> Prac 3 Task 13 <?php

$conn = mysql_connect(“localhost”, “######”, “#######”);
mysql_select_db(“warehouse051”, $conn)
or die ('Database not found ’ . mysql_error() );
$sql = “SELECT * FROM customer”;
$rs = mysql_query($sql, $conn)
or die (‘Problem with query’ . mysql_error());

if (isset($_POST[‘customerid’], $_POST[‘customerfname’], $_POST[‘customerlname’],$_POST[‘customeraddress’], $_POST[‘state’], $_POST[‘postcode’] ))
{
$errors = array();

$customerid = $_POST[‘customerid’];
$customerfname = $_POST[‘customerfname’];
$customerlname = $_POST[‘customerlname’];
$customeraddress = $_POST[‘customeraddress’];
$state = $_POST[‘state’];
$postcode = $_POST[‘postcode’];

//checks each element of the form

if(empty($customerid) || empty($customerfname) || empty($customerlname))
{
$errors[] = ‘All field must be filled out’;
}else{

if(!is_numeric($customerid))
{
$errors[] = ‘Enter a 6 valid number’;
}else {
if(strlen($customerid) != 6)
{
$errors[] = ‘Please enter a 6 number ID’;
}
}

if(is_numeric($customerfname) || is_numeric($customerlname))
{
$errors[] = ‘Enter a proper customer name’;
}else if(strlen($customerfname > 30 ) || strlen($customerlname > 30))
{
$errors[] = ‘Customer name must be less than 30 characters’;
}

if(!$customeraddress == “” && $state == “select”)
{
$errors[] = ‘Please select a state’;
}

if(!$customeraddress == “” && $postcode == “”)
{
$errors[] = ‘Enter postcode’;
}else if(!is_numeric($postcode) || strlen($postcode) != 4 )
{
$errors[] = ‘Enter a proper postcode’;
}

$query = “select * from customer where customerID = '” . $customerid . "’ ";
$res = mysql_query($query) or die (“ERROR: sql = “.$query.”
”.mysql_error());
$row = mysql_fetch_array($res); // reading only one result (we don’t care if more than one)
if ($row[‘customerID’] == $customerid)
{
echo (“Already exists”);
}
else
{
mysql_query(“INSERT INTO customer(customerID) VALUES (’$customerid’, ‘$customerfname’, ‘$customerlname’, ‘$customeraddress’, ‘$state’, ‘$postcode’)”);
}

}

if(!empty($errors))
{
foreach ($errors as $error) {
echo ‘’,$error , ‘
’;

   }	  

}else {
echo ‘This customer has now been registered’;

}

}

?>

Customer Information Collection

	<td><label for="suburb"> Suburb: </label></td>
<td><input type="text" id="suburb" name="suburb"/></td>
Customer ID (integer value):
Customer Frist Name:
Customer Last Name:
Customer Address:
State: -- ACT NSW NT QLD SA TAS VIC WA Post Code:

 

[/php]

The problem lies in the logic of your script. If the customer exists, you’re echoing rather than adding to $errors. Surely you’d want to add to $errors to prevent the message that pops up when no errors have been found from occuring…

Sponsor our Newsletter | Privacy Policy | Terms of Service