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?
<? 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
}
?>