Thank you that worked.. I have a problem now when you press the submit button on the form, it renders the form twice, or if you fill out the information i'm getting headers already sent.. I know it must be down to my if statements somewhere.
<?php
/*
NEW.PHP
Allows user to create a new entry in the database
*/
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($Name, $Rank, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New Record</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="wrapper">
<p>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
</p>
<p> </p>
<form action="" method="post">
<div>
<table width="842" border="0" class="styletable" style="border: 1px solid #000000">
<tr>
<td width="255"><strong>Name: *</strong></td>
<td width="577"><input type="text" name="Name" value="<?php echo $Name; ?>" /></td>
</tr>
<tr>
<td><strong>Rank:</strong></td>
<td><input type="text" name="Rank" value="<?php echo $Rank; ?>" /></td>
</tr>
<tr>
<td><strong>Contact Email: *</strong></td>
<td><input type="text" name="ContactEmail" value="<?php echo $ContactEmail; ?>" /></td>
</tr>
<tr>
<td><strong>Website: (omitting http://)</strong></td>
<td><input type="text" name="Website" value="<?php echo $Website; ?>" /></td>
</tr>
<tr>
<td><strong>Location:</strong></td>
<td><select name="Location" id="Location">
<option value="East Midlands">East Midlands</option>
<option value="East Of England">East Of England</option>
<option value="Greater London">Greater London</option>
<option value="North East England">North East England</option>
<option value="North West England">North West England</option>
<option value="South East England">South East England</option>
<option value="South West England">Soth West England</option>
<option value="West Midlands">West Midlands</option>
<option value="Yorkshire And The Humber">Yorkshire And The Humber</option>
</select> </td>
</tr>
<tr>
<td><strong>Bio:</strong></td>
<td><textarea name="BIO" id="BIO" cols="60" rows="5"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<p>* required</p>
<input type="submit" name="submit" value="Submit"> <a href="view.php">Cancel - View Records</a>
</div>
</form>
</div>
</body>
</html>
<?php
}
// connect to the database
include('../connect-db.php');
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$Name = mysql_real_escape_string(htmlspecialchars($_POST['Name']));
$Rank = mysql_real_escape_string(htmlspecialchars($_POST['Rank']));
$ContactEmail = mysql_real_escape_string(htmlspecialchars($_POST['ContactEmail']));
$Website = mysql_real_escape_string(htmlspecialchars($_POST['Website']));
$Location = mysql_real_escape_string(htmlspecialchars($_POST['Location']));
$BIO = mysql_real_escape_string(htmlspecialchars($_POST['BIO']));
// check to make sure both fields are entered
if ($Name == '' || $ContactEmail == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($Name, $Rank, $error);
}
else
{
// save the data to the database
mysql_query("INSERT members SET Name='$Name', Rank='$Rank',ContactEmail='$ContactEmail', Website='$Website', Location='$Location', BIO='$BIO'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
// if the form hasn't been submitted, display the form
}}
renderForm('','','');
?>