Validate email works, but now how to transition to input into database?

Discussion in 'PHP' started by Mitchell, Jun 15, 2010.

  1. #1
    I am having trouble with the if statement after validate email. My database excepts the unvalidated email input, but not the validated email input. The validation seems to work, but the if statement after exiting the validation function is where I am having trouble. How do I make that transition so all three (title, discription and email) are ready to be input into database? Thanks.

    
    <?php //new_post06.php
        include 'npfunctions04.php';
        nukeMagicQuotes();
    
        // create short variable names    
        if (isset($_POST['title'])) $title=sanitizeString($_POST['title']);
        if (isset($_POST['discription'])) $discription=sanitizeString($_POST['discription']);
        if (isset($_POST['email'])) $email=sanitizeString($_POST['email']);
        
        $email = validate_email($email);
        
        if ($email == "no"){ echo "No Email was entered<br />";}    
            else if ($email == "invalid"){ echo "The Email address is invalid<br />";}
                /*else{ echo "Form data successfully validated: $email.";}*/
                else{$email = $email;}
        exit;
        
            
    
        if (!$title || !$discription) {
            echo "You have not entered all the required details.<br />"
            ."Please go back and try again.";
            exit;
        }
    
        @ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals03');
    
        if (mysqli_connect_errno()) {
        echo "Error: Could not connect to database.  Please try again later.";
        exit;
        }
    
        $query = "insert into mammals03 (title, discription, email) values (?, ?, ?)";
        $stmt = $db->stmt_init();
        if ($stmt->prepare($query)) {
            $stmt->bind_param('sss', $title, $discription, $email);
            $stmt->execute();
            }
            
        $db->close();
    ?>
    
    PHP:
    
    <?php //npfunctions04.php
    function sanitizeString($var)
    {
        $var = strip_tags($var);
        $var = htmlentities($var);
        return $var;
    }
    
    function nukeMagicQuotes() {
      if (get_magic_quotes_gpc()) {
        function stripslashes_deep($value) {
          $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
          return $value;
          }
        $_POST = array_map('stripslashes_deep', $_POST);
        $_GET = array_map('stripslashes_deep', $_GET);
        $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
        }
      }
      
    function validate_email($email) {
        if ($email == "") return "no";
            else if (!((strpos($email, ".") > 0) &&
                       (strpos($email, "@") > 0)) ||
                        preg_match("/[^a-zA-Z0-9.@_-]/", $email))
            return "invalid";
        return $email;
        }
    ?>
    
    PHP:
     
    Mitchell, Jun 15, 2010 IP
  2. Chronomus

    Chronomus Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You seem to have an extra exit; after the email validation if statement, that would be executed regardless of any conditions.
     
    Chronomus, Jun 15, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. That worked. I don't understand why, I only see one exit; But then what do I know, I'm new.

    
    $email = validate_email($email);
        
        if ($email == "no"){ echo "No Email was entered<br />";}    
            else if ($email == "invalid"){ echo "The Email address is invalid<br />";}
                /*else{ echo "Form data successfully validated: $email.";}*/
                else{$email = $email;}
        exit;
    PHP:
     
    Mitchell, Jun 15, 2010 IP
  4. Chronomus

    Chronomus Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well because that exit; will stop the execution of the whole script after that if statement. I think you misplaced it, you were probably supposed to put it in the if statement when the email comes out as invalid, to stop the script from continuing.
    $email = validate_email($email);
       
        if ($email == "no"){ echo "No Email was entered<br />"; exit;}    
            else if ($email == "invalid"){ echo "The Email address is invalid<br />"; exit;}
                /*else{ echo "Form data successfully validated: $email.";}*/
                else{$email = $email;}
    Code (markup):
     
    Chronomus, Jun 15, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I see. Thanks I am glad to learn something today.
     
    Mitchell, Jun 15, 2010 IP