Skip to main content
Inspiring
November 4, 2013
Question

Validating email blues

  • November 4, 2013
  • 3 replies
  • 2977 views

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 and the information is submitted successfully. What is going on? Thanks for your help in advanced!

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

Participating Frequently
November 5, 2013

I'm not a php code, but checking the manual for filter_var

http://php.net/manual/en/function.filter-var.php

it seems that it returns returns the filtered value, or FALSE if the filter fails. So you don't want to test for the boolean TRUE .

Inspiring
November 7, 2013

Hello,

My other forms has the boolean to true and it works. I switched to FALSE and it's giving me the same error. anyone?

Participating Frequently
November 7, 2013

>My other forms has the boolean to true and it works.

We'd need to see the code for that. Is that running on the same server?

>I switched to FALSE and it's giving me the same error. anyone?

That's not going to work. Try using echo statements to examine your variables.

You can also try using this instead:

if(filter_var($email, FILTER_VALIDATE_EMAIL)){