# Help with Validation & Else Statement

**URL:** https://forum.kirupa.com/t/help-with-validation-else-statement/220233
**Category:** programming
**Created:** [March 23, 2007, 10:47pm UTC](https://forum.kirupa.com/t/help-with-validation-else-statement/220233 "2007-03-23T22:47:16Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![opel](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/opel/32/199_2.png) [@opel](https://forum.kirupa.com/u/opel)
#### Post date: [March 23, 2007, 10:47pm UTC](https://forum.kirupa.com/t/help-with-validation-else-statement/220233/1 "2007-03-23T22:47:16Z")

</div>

Not been on the forum for a while as have been busy with projects, I have been trying to teach myself how to code PHP by hand ( no more Dreamweaver) and am going through the basics.

I have set up a single table that has an id and name field and I am trying to insert a value into it. This all works fine however I have tried t add in some validation, that displays an error message is no value is entered and submit is hit.

Below is my code, I am not receivine any errors however my else statement is not displaying the HTML input form like it should?

```php
<? if (isset($_POST['submit'])) {

 
   //connection script

$database = "test DB";
$host = "localhost";
$username	= "root";
$password	= "password";

$connection = mysql_connect($host, $username, $password); 
mysql_select_db($database, $connection); 

if(!$_POST['name']) {
            echo "You must enter a name !";
            exit;
        }

elseif($_POST['name']) {

   //convert all the posts to variables:
   $name = $_POST['name'];

   //Insert the values into the correct database with the right fields
   $result=MYSQL_QUERY("INSERT INTO brands (name)".
      "VALUES ('".$_POST['name']."')"); 

    //confirm
   echo "You must have entered a name,go back to the <a href=\"brands_list.php\">list</a>"; 
	}
else {
// close php so we can put in our code
?>
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<TABLE>
<TR>
   <TD>Title:</TD>
   <TD><INPUT TYPE='TEXT' class="input" NAME='name' size=60></TD>
</TR>

<TR>
   <TD></TD><br>
   <TD><INPUT TYPE="submit" name="submit" class="button" value="submit"></TD> 
</TR>
</TABLE>
</form>

<?
 } //close the else statement
}
?>

```
