0

I have a quiz form that is connected to my database, however I need to prevent duplicate email entries being inserted. I have tried the following:

//Check for duplicate email addresses function checkEmail($email){ $sql = DB::select('email')->from('myquiz')->where('email','=','$email')->execute(); $result = mysql_result(mysql_query($sql),0) ; if( $result > 0 ){ die( "There is already a user with that email!" ) ; }//end if } 

But I 'm still getting duplicate entries, here is all my code (may be I'm not running this in the correct place?)

 public function action_myquiz() { $this->template->styles['assets/css/myquiz.css'] = 'screen'; $this->template->jscripts[] = 'assets/scripts/myquiz.js'; $this->template->content = View::factory('quiz/myquiz'); $this->template->content->thanks = false; if ($this->request->post('entry')) { $post = $this->request->post('entry'); //Check for duplicate email addresses function checkEmail($email){ $sql = DB::select('email')->from('myquiz')->where('email','=','$email')->execute(); $result = mysql_result(mysql_query($sql),0) ; if( $result > 0 ){ die( "There is already a user with that email!" ) ; }//end if } // save participant's info $stmt = DB::query(Database::INSERT, 'INSERT INTO `myquiz` (`first_name`, `last_name`, `email`, `confirm_email`) VALUES (:first_name, :last_name, :email, :confirm_email)'); $stmt->param(':first_name', $post['first_name']); $stmt->param(':last_name', $post['last_name']); $stmt->param(':email', $post['email']); $stmt->param(':confirm_email', $post['confirm_email']); try { $stmt->execute(); // var_dump($post); } catch (Exception $e) { FB::error($e); } $this->template->content->thanks = true; } } 
4
  • 2
    Rather than checking if email exists prior to insertion I would advise attempt to add in a try/catch block. Catch duplicate key exception and then indicate that the email is duplicate. Commented Mar 31, 2014 at 9:43
  • 1
    or make the email-field UNIQUE and then try catch like Raj said Commented Mar 31, 2014 at 9:46
  • 3
    also make sure you wrap vars in double quotes '$email' should be "'$email'" or just $email. You cannot parse vars inside single quotes Commented Mar 31, 2014 at 9:47
  • Thanks! I'm going to go with Raj's option as i think it would be the best :) Commented Mar 31, 2014 at 9:58

4 Answers 4

2

Two problems:

  1. You're never calling your checkEmail() function so it's never running. You should either remove that code from the function or just call the function where it needs to run.
  2. In that function you're checking that no email exists that literally equals "$email". PHP will only parse variables in double quotes - change that line to use where('email','=',"$email") instead.
Sign up to request clarification or add additional context in comments.

Comments

0

Change mysql_result to mysql_num_rows as below first function and try.

$result = mysql_num_rows(mysql_query($sql),0) ; 

Comments

0

Your function is never executed. You will need to define the function outside the action_myquiz function and then call it. Also in the 'where' clause your not passing the email address in correctly and you can just use 'mysql_num_rows' to return the number of rows.

Try this:

//Check for duplicate email addresses private function checkEmail($email) { $sql = DB::select('email')->from('myquiz')->where('email', '=', $email)->execute(); $result = mysql_num_rows(mysql_query($sql),0) ; if( $result > 0 ) { die( "There is already a user with that email!" ) ; } } public function action_myquiz() { $this->template->styles['assets/css/myquiz.css'] = 'screen'; $this->template->jscripts[] = 'assets/scripts/myquiz.js'; $this->template->content = View::factory('quiz/myquiz'); $this->template->content->thanks = false; if ($this->request->post('entry')) { $post = $this->request->post('entry'); // Check if email exists $this->checkEmail($_POST['email']); // save participant's info $stmt = DB::query(Database::INSERT, 'INSERT INTO `myquiz` (`first_name`, `last_name`, `email`, `confirm_email`) VALUES (:first_name, :last_name, :email, :confirm_email)'); $stmt->param(':first_name', $post['first_name']); $stmt->param(':last_name', $post['last_name']); $stmt->param(':email', $post['email']); $stmt->param(':confirm_email', $post['confirm_email']); try { $stmt->execute(); // var_dump($post); } catch (Exception $e) { FB::error($e); } $this->template->content->thanks = true; } } 

A couple of additional points:

  • Raj is correct that a Try/Catch block might be better
  • Ensure your data is escaped before passing into SQL queries, your framework might be doing this for you.

Comments

0

In PHP you can't place a function inside another function. So you need to place it outside your action_myquiz function. You'll also want to change mysql_result to mysql_num_rows. Something like this

//Check for duplicate email addresses function checkEmail($email){ $sql = DB::select('email')->from('myquiz')->where('email','=',"$email")->execute(); $result = mysql_num_rows(mysql_query($sql),0) ; if( $result > 0 ){ return true; } return false; } //Now start your other function function checkEmail($email){ 

Then inside your action_myquiz function you need to call your checkEmail function. Like

if(checkEmail($email) === false) { //Proceed with insert } else { //Don't do insert } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.