SitePoint Sponsor

User Tag List

Results 1 to 25 of 38

Threaded View

  1. #1
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    626
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question Script Captures Duplicate E-mail Entry in Database, but Doesn't Display User-end Msg

    Hello everyone,

    I'm back with another brain buster!

    This time, I'm checking the database using a PDO connection for a duplicate email address in a column, emailaddress. The problem is not in finding out if the e-mail address is a duplicate entry. The problem is displaying the error message to the user (not just seeing it in Firebug).

    All the other error messages this script detects are properly appended to a <p> element with an i.d. of errormessage. For some strange reason, this error goes unpublished and the form just sits there on submission. For example, if the user doesn't input a name, an error message will appear saying, "Please enter a name".

    Here's why I know the query and the detection of the duplicate entry is not the issue. I had set up the script to echo the variable, $dbCode, which is used to detect the results of the database writing. 3 corresponds to the e-mail being a duplicate (as you'll see), and that is what is being returned and echoed into Firebug/developer tools. Not only that, but the error message reads, "That e-mail address already exists", just like it should, as well as validation set to failure and database set to failure in my $response array that is sent back to JavaScript. Everything seems in place!

    confirmform.php (the file called by JavaScript):
    PHP Code:
    <?php
        $instance 
    = new CheckForm;
        
    $instance -> checkSubmission();
        
        class 
    CheckForm
        
    {
            public function 
    checkSubmission()
            {    
                
    $origEmail $_POST['origEmail'];
                
    $confirmEmail htmlspecialchars($_POST['confirmEmail']);
                
    $name htmlspecialchars($_POST['name']);
                
    $ageRange $_POST['age'];
                
    $gender $_POST['gender'];
                
    $country $_POST['country'];
                
    $catcher htmlspecialchars($_POST['catcher']);
                
    $mathAnswer htmlspecialchars($_POST['addition']);
                
    $rightAnswer $_POST['mathAnswer'];
                
    $submissionTime $_POST['submissionTime'];
                
    $status 0;
            
                
    $response = array("validation" => " ""message" => " ""database" => " ");
                
                if (empty(
    $confirmEmail) && empty($name) && $country === "Select Country") {
                    
    $response['message'] = "That's not a valid submission.";
                } elseif (empty(
    $confirmEmail) && $country === "Select Country"){
                    
    $response['message'] = "Please confirm your e-mail and select a location.";
                } elseif (empty(
    $name) && $country === "Select Country"){
                    
    $response['message'] = "Please enter a name and select a location.";
                } elseif (empty(
    $name)) {
                    
    $response['message'] = "Please enter a name.";
                } elseif (empty(
    $confirmEmail)) {
                    
    $response['message'] = "No confirmation e-mail was entered.";
                } elseif (
    $origEmail != $confirmEmail) {
                    
    $response['message'] = "E-mail addresses don't match.";
                } elseif (
    $country === "Select Country") { 
                    
    $response['message'] = "Please select a location.";
                } elseif (
    $mathAnswer != $rightAnswer) {
                    
    $response['message'] = "Math answer is incorrect.";
                } elseif (!empty(
    $catcher)) {
                    
    $response['message'] = "Bot submission.";
                } elseif (
    $submissionTime <= 8000) {
                    
    $response['message'] = "Woah! Slow down and fill out the form.";
                } else
                    
    $status 1;
                    
                if (
    $gender === "Male")
                    
    $gender "M";
                elseif (
    $gender === "Female")
                    
    $gender "F";
                else
                    
    $gender NULL;

                
                if (
    $status === 1) {
                    require_once(
    "categoryfinder.php");
                    
    $categoryFinder = new CategoryFinder;
                    
    $category $categoryFinder -> getCategory();
                    
    $response['validation'] = "pass";
                    
    $response['message'] = "Thanks for joining the e-mail list, <b>" $name "</b>, under the e-mail address, <b>" $confirmEmail "</b>.";
                            
                    require_once(
    'databasewriter.php');
                    
    $dbWriter = new DatabaseWriter;
                    
    $dbCode $dbWriter -> writeUserToDatabase($confirmEmail$name$ageRange$gender$country$category);
                    

                                   
    // I BELIEVE THE PROBLEM IS HERE SOMEWHERE
                    
    if ($dbCode === 1) {
                        
    $response['database'] = "pass";
                        echo 
    'Database Write Successful';
                    } else {
                        
    $response['database'] = "fail";
                        
    $response['validation'] = "fail";
                        echo 
    'Database Write Failure';
                    }
                    if (
    $dbCode === 2) {
                        
    $response['message'] = "Server error. Please try again later.";
                    } elseif (
    $dbCode === 3) {
                        
    $response['message'] = "That e-mail address already exists.";
                    }
                }
            
                echo 
    json_encode($response);
            }
        }
    ?>
    databasewriter.php
    PHP Code:
    <?php
        
    class DatabaseWriter
        
    {
            public function 
    writeUserToDatabase($email $name $age $gender $country $category)
            {    
                
    $host 'test.testmysql.com';
                
    $dbname 'testname';
                
    $user 'testusername';
                
    $pass 'mypassword';
                
                
    $connection = new PDO("mysql:host=" $host ";dbname=" $dbname$user$pass);
                
                try {
                    
    $connection -> setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
                    
    $statement $connection -> prepare("INSERT INTO emailcollection (emailaddress, name, age, gender, country, category) VALUES (:emailaddress, :name, :age, :gender, :country, :category)");
                    
    $statement -> bindValue(':emailaddress'$email);
                    
    $statement -> bindValue(':name'$name);
                    
    $statement -> bindValue(':age'$age);
                    
    $statement -> bindValue(':gender'$gender);
                    
    $statement -> bindValue(':country'$country);
                    
    $statement -> bindValue(':category'$category);
                    
    $statement -> execute();
            
                    return 
    1;
                }catch (
    PDOException $e){
                    
    $dupeQuery $connection -> prepare("SELECT * FROM emailcollection WHERE emailaddress = '" $email "'");
                    
    $dupeQuery -> execute();
                    
    $number $dupeQuery -> rowCount();
                    echo 
    ' number of rows:     ' $number '    ';
                    if (
    $number 0){
                        return 
    3;
                    } else {
                        
    //echo $e -> getMessage();
                        
    return 2;
                    }
                }
                
    $connection NULL;
            }
        }
    ?>
    Many thanks,
    Tyler
    Last edited by ralph.m; Mar 31, 2013 at 00:26. Reason: removed security info


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •