Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查数据库中是否已存在电子邮件_Php_Mysql_Sql_Email - Fatal编程技术网

Php 检查数据库中是否已存在电子邮件

Php 检查数据库中是否已存在电子邮件,php,mysql,sql,email,Php,Mysql,Sql,Email,我有一个连接到数据库的测验表单,但是我需要防止重复的电子邮件条目被插入。 我尝试了以下方法: //Check for duplicate email addresses function checkEmail($email){ $sql = DB::select('email')->from('myquiz')->where('email','=','$email')->execute();

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

//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
            }
但是我仍然得到重复的条目,这是我所有的代码(可能是我没有在正确的位置运行它?)

两个问题:

  • 您从未调用
    checkEmail()
    函数,因此它从未运行过。您应该从函数中删除该代码,或者只调用需要运行的函数
  • 在该函数中,您将检查是否不存在与“$email”完全相等的电子邮件。PHP将只解析双引号中的变量-将该行改为使用
    where('email','=',“$email”)

  • 将mysql\u结果更改为mysql\u num\u行,如下面的第一个函数所示,然后重试

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

    您的函数从未执行过。您需要在action_myquick函数之外定义函数,然后调用它。同样在'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查询之前转义数据,您的框架可能会为您这样做

      • 在PHP中,不能将一个函数放在另一个函数中。因此,您需要将其放置在
        action\u myquick
        函数之外。您还需要将
        mysql\u结果
        更改为
        mysql\u num\u行
        。像这样的

        //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\u myquick
        函数中,您需要调用
        checkEmail
        函数。像

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

        我建议尝试添加一个try/catch块,而不是在插入之前检查电子邮件是否存在。捕获重复密钥异常,然后指出电子邮件是重复的。或者使电子邮件字段唯一,然后尝试像Raj saidalso一样捕获。因此,请确保将变量用双引号括起来
        “$email”
        应该是
        “$email”
        或者只是
        $email
        。你不能在单引号内解析变量!我会同意拉杰的选择,因为我认为这是最好的:)
        if(checkEmail($email) === false) {
            //Proceed with insert
        } else {
            //Don't do insert
        }