Okay, I'm working a database project using php and mysql. Just to save time re-writing my code, I copied and pasted into the editor so I can make my changes to finish the project on time. There is an issue though and not sure why it's giving me an error. I entered a valid email addresss on the email field on the form. Before I go into further details, here is my link and code.
<?php
if (isset($_POST['submitted'])){
require_once('Connections2/orderform.php');
$fields = array(
'name',
'email',
);
foreach($fields as $fieldName) {
if(isset($_POST[$fieldName]) and safe(trim(stripslashes($_POST[$fieldName]))) !==''){
$$fieldName = safe(trim(stripslashes($_POST[$fieldName])));
}else {
$errors[] = "Please enter your". $fieldName .""; //code to validate fields
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) == true){
} else {
$errors[] = 'Please enter a valid email address';
}
}
if(!isset($errors)){
$query = "SELECT id FROM orders WHERE email='$email'";
$result = mysql_query($query);
if(mysql_num_rows($result) == 0) {
$query = "INSERT INTO orders (email, name, address, city, state, zip, product, amount, instructions, date)
VALUES ('$email', '$name', '$address', '$city', '$state', '$zip,'$product', '$amount', '$instructions', NOW())"; //databasse connection
$result = mysql_query ($query);
if ($result){
$url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') || (substr($url, -1) == '\\')) {
$url = substr ($url, 0, -1);
}
$url .= '/ordersubmitted.php';
header("Location: $url"); //This is the line of error that is reporting after submitting to the database
exit();
} else {
$errors[] = 'You could not be registered due to a system error. We apologize for any inconvenience.';
$errors[] = mysql_error() . '<br /><br />Query: ' . $query;
}
} else {
$errors[] = 'The email address has already been registered.';
}
}
mysql_close();
} else {
$errors = NULL;
}
if (!empty($errors)){
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach($errors as $msg) {
echo " - $msg<br/>\n";
}
echo '</p><p>Please try again.</p><p><br/></p>';
}
function safe($string)
{
$pattern = "/\r|\n|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i";
return preg_replace($pattern, '', $string);
}
?>
http://www.rodriguezstudios.com/orderform
The error is coming from this line of code
if(filter_var($email, FILTER_VALIDATE_EMAIL) == true){
} else {
$errors[] = 'Please enter a valid email address';
}
}
The html email field is
<input name="email" type="text" id="email">
But i checked the other forms on the same website (different database accounts) that has that same code fragment and the information is submitted successfully. What is going on? Thanks for your help in advanced!