2014-03-31 287 views
0

我有一个连接到我的数据库的测验表单,但是我需要防止插入重复的电子邮件条目。 我曾尝试以下:检查数据库中是否已存在电子邮件

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

而不是检查电子邮件是否存在之前插入我会建议尝试添加一个try/catch块。捕获重复的关键异常,然后指出该电子邮件是重复的。 – Raj

+1

或使电子邮件字段UNIQUE,然后尝试赶上像拉吉说 –

+3

也确保你用双引号包装变量''$ email''应该是'''$ email'“'或只是'$ email'。你不能在单引号内解析变量 – mic

回答

2

两个问题:

  1. 你永远不会打电话给你的checkEmail()函数,所以它永远不会运行。您应该从函数中删除该代码,或者只是在需要运行的地方调用该函数。
  2. 在那个函数中,你检查没有电子邮件存在,字面上等于“$ email”。 PHP将只使用双引号解析变量 - 将该行更改为使用where('email','=',"$email")
0

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

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

您的功能从未执行。您需要在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; 
    } 
} 

一对夫妇的附加分:

  • 拉吉是正确的,一个try/catch块可能会传递到SQL之前更好
  • 确保您的数据逃脱查询,你的框架可能会为你做这件事。
0

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

​​

东西然后你action_myquiz功能里面你需要打电话给你checkEmail功能。像

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