# Help with my php form?

**URL:** <https://www.phphelp.com/t/help-with-my-php-form/20212>\
**Category:** Beginners - Learning PHP\
**Created:** [May 6, 2013, 10:08am UTC](https://www.phphelp.com/t/help-with-my-php-form/20212 "2013-05-06T10:08:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sujath](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@Sujath](https://www.phphelp.com/u/Sujath)\
**Post date:** [May 6, 2013, 10:08am UTC](https://www.phphelp.com/t/help-with-my-php-form/20212/1 "2013-05-06T10:08:09Z")

</div>

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: |

&nbsp;

[/php]

---

<div class="post-metadata">

**Author:** ![sebrenauld](https://www.phphelp.com/user_avatar/www.phphelp.com/sebrenauld/32/391_2.png) [@sebrenauld](https://www.phphelp.com/u/sebrenauld)\
**Post date:** [May 6, 2013, 12:26pm UTC](https://www.phphelp.com/t/help-with-my-php-form/20212/2 "2013-05-06T12:26:01Z")

</div>

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…
