【问题标题】:Check if email already exists in database检查电子邮件是否已存在于数据库中
【发布时间】:2014-05-10 14:24:15
【问题描述】:

我有一个连接到我的数据库的测验表格,但是我需要防止插入重复的电子邮件条目。 我尝试了以下方法:

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

但我仍然收到重复的条目,这是我所有的代码(可能是我没有在正确的地方运行它?)

 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;
        }
    }

【问题讨论】:

  • 与其在插入之前检查电子邮件是否存在,我建议尝试添加一个 try/catch 块。捕获重复键异常,然后表明该电子邮件是重复的。
  • 或将电子邮件字段设为唯一,然后尝试像 Raj 所说的那样捕获
  • 还要确保将变量用双引号括起来 '$email' 应该是 "'$email'" 或只是 $email。您无法解析单引号内的变量
  • 谢谢!我会选择 Raj 的选项,因为我认为这将是最好的 :)

标签: php mysql sql email


【解决方案1】:

两个问题:

  1. 您永远不会调用您的checkEmail() 函数,因此它永远不会运行。您应该从函数中删除该代码,或者只在需要运行的地方调用该函数。
  2. 在该函数中,您正在检查是否不存在与“$email”字面相同的电子邮件。 PHP 只会解析双引号中的变量 - 将该行更改为使用 where('email','=',"$email")

【讨论】:

    【解决方案2】:

    将 mysql_result 更改为 mysql_num_rows 如下第一个函数并尝试。

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

    【讨论】:

      【解决方案3】:

      你的函数永远不会被执行。您需要在 action_myquiz 函数之外定义该函数,然后调用它。同样在“where”子句中,您没有正确传递电子邮件地址,您可以使用“mysql_num_rows”返回行数。

      试试这个:

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

      补充几点:

      • Raj 是正确的,Try/Catch 块可能更好
      • 确保您的数据在传递到 SQL 查询之前已转义,您的框架可能会为您执行此操作。

      【讨论】:

        【解决方案4】:

        在 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){
        

        然后在您的action_myquiz 函数中,您需要调用您的checkEmail 函数。喜欢

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

        【讨论】:

          猜你喜欢
          • 2019-09-17
          • 2014-03-15
          • 1970-01-01
          • 1970-01-01
          • 2013-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多