检查电子邮件是否已经存在于数据库中。

时间:2022-09-25 23:51:41

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 个解决方案

#1


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. 你永远不会调用你的checkEmail()函数,所以它永远不会运行。您应该从函数中删除该代码,或者只调用需要运行的函数。
  3. 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.
  4. 在这个函数中,你要检查没有电子邮件存在,字面上等于“$email”。PHP只会对双引号中的变量进行解析——改为使用where('email','=', ' $email ')。

#2


0  

Change mysql_result to mysql_num_rows as below first function and try.

将mysql_result更改为mysql_num_rows,如下所示。

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

#3


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.

你的函数永远不会被执行。您需要在action_myquiz函数之外定义函数,然后调用它。在“where”子句中,您不能正确地传递电子邮件地址,您可以使用“mysql_num_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
  • 拉杰是正确的,一个Try/Catch块可能更好。
  • Ensure your data is escaped before passing into SQL queries, your framework might be doing this for you.
  • 在传递到SQL查询之前,确保您的数据被转义,您的框架可能会为您这样做。

#4


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

在PHP中,你不能把函数放在另一个函数中。所以你需要把它放到action_myquiz函数外面。您还需要将mysql_result更改为mysql_num_rows。像这样的东西

//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

然后,在action_myquiz函数中,需要调用checkEmail函数。就像

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

#1


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. 你永远不会调用你的checkEmail()函数,所以它永远不会运行。您应该从函数中删除该代码,或者只调用需要运行的函数。
  3. 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.
  4. 在这个函数中,你要检查没有电子邮件存在,字面上等于“$email”。PHP只会对双引号中的变量进行解析——改为使用where('email','=', ' $email ')。

#2


0  

Change mysql_result to mysql_num_rows as below first function and try.

将mysql_result更改为mysql_num_rows,如下所示。

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

#3


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.

你的函数永远不会被执行。您需要在action_myquiz函数之外定义函数,然后调用它。在“where”子句中,您不能正确地传递电子邮件地址,您可以使用“mysql_num_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
  • 拉杰是正确的,一个Try/Catch块可能更好。
  • Ensure your data is escaped before passing into SQL queries, your framework might be doing this for you.
  • 在传递到SQL查询之前,确保您的数据被转义,您的框架可能会为您这样做。

#4


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

在PHP中,你不能把函数放在另一个函数中。所以你需要把它放到action_myquiz函数外面。您还需要将mysql_result更改为mysql_num_rows。像这样的东西

//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

然后,在action_myquiz函数中,需要调用checkEmail函数。就像

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