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; } }
'$email'should be"'$email'"or just$email. You cannot parse vars inside single quotes