Hi there,
I thought i’d sorted my php validation out. however, if I enter the same name twice, i get a ‘name already taken’, which is fine. If I leave the name as-is, and correctly enter all other fields, the name is ignored, and the data inserted to the database. Meaning i now have 2 of the same name being input to the db.
Can anyone help? I have inserted my form (html and php) below.
<body>
<div id="container">
<div id="header"> </div>
<div id="navigation">
<ul>
<li><a href="index.php">Home </a></li>
<li><a href="about.php"><img src="http://www.sitepoint.com/forums/images/about.jpg"> </a></li>
<li><a href="register.php">Register </a></li>
<li><a href="help.php">Help </a></li>
<li><a href="faq.php">FAQ </a></li>
</ul>
</div>
<div id="left_content">
</div>
<div id="main_content">
<h3> Hello and Welcome </h3>
<?php
function account_exists($column, $value) {
$accounts = mysql_query('SELECT * FROM businesses WHERE ' . $column . ' = "' . $value . '"');
$return = (mysql_num_rows($accounts) > 0) ? true : false;
mysql_free_result($accounts);
return $return;
}
$spam_check = ZGF;
// Check Email Address courtesy of Dave Child:
// http://www.ilovejackdaniels.com/php/email-address-validation/
function check_email_address($email) {
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) return false;
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\\.-]{0,63})|(\\"[^(\\\\|\\")]{0,62}\\"))$", $local_array[$i])) return false;
}
if (!ereg("^\\[?[0-9\\.]+\\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) return false;
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) return false;
}
}
return true;
}
if (isset($_POST['submit'])) {
$business_name = ( $_POST['business_name']);
$category = ( $_POST['category']);
$email = ( $_POST['email'] );
$description = ( $_POST['description'] );
$spam_check = $_REQUEST['spam_check'];
if (empty($business_name)) {
echo "<p class='form_text'>The Business Name is empty</p>";
}
else if (account_exists('business_name', $business_name)) {
echo "<p class='form_text'> Sorry, that Business Name is already registered";
}
if (empty($category)) {
echo "<p class='form_text'>The Category is empty</p>";
}
// Email address checks
if (empty($email)) {
echo "<p class='form_text'>You must enter your email address</p>";
} else if (!check_email_address($email)) {
echo "<p class='form_text'>Your email address is invalid</p>";
}
if (empty($description)) {
echo "<p class='form_text'>The Description is empty</p>";
}
if ($spam_check !="ZGF") {
echo "<p class='form_text'>Spam Check Failed</p>";
}
else {
@mysql_query( "INSERT INTO `businesses`(business_name,category,email,description) VALUES ('$business_name','$category','$email','$description')" ) or die( mysql_error() );
{//send email
$business_name=stripslashes($_REQUEST['business_name']) ;
$email=stripslashes($_REQUEST['email']) ;
$category=stripslashes($_REQUEST['category']) ;
$description=stripslashes($_REQUEST['description']) ;
$email_message = "Business Name: \
{$business_name} \
\
Email Address: \
{$email} \
\
Category: \
{$category} \
\
Description: \
{$description}";
mail('testemail@test.com', 'Business Reg Submission', $email_message, 'From: '.$email."\\r\
");
}
echo "<p class='form_text_success'>Thanks, you have now been added to the directory.</p>";
echo '<META HTTP-EQUIV="Refresh" Content="5; URL=thanks.php">';
}
}
?>
<div id="stylized" class="myform">
<fieldset>
<form action="index.php" method="post">
<h1>Business Registration</h1>
<p>Please enter the form below to register your business with business-reg</p>
<label>Business Name
<span class="small">Add your business name</span>
</label>
<input type="text" name="business_name" id="business_name" value="<?php echo $business_name; ?>" />
<label> Category
<span class="small">Please add a category</span>
</label>
<select name="category" size="1" id="category">
<option value="accomodation">Accomodation</option>
<option value="accountancy">Accountancy</option>
<option value="accomodation">Accomodation</option>
</select>
<label>Business Email
<span class="small">Add a valid address</span>
</label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
<label>Description
<span class="small">Please describe your business</span>
</label>
<textarea cols="22" rows="10" id="description" name="description"><?php echo $description; ?></textarea>
<label>Spam Checker
<span class="small">Enter the last 3 characters</span>
<img src="http://www.sitepoint.com/forums/images/spam.jpg">
</label>
<input type="text" name="spam_check" id="spam_check" />
<button type="submit" name="submit" id="submit"></button>
<div class="spacer"></div>
</form>
</fieldset>
</div>
</div>
</div>
</body>