我有一个连接到我的数据库的测验表单,但我需要阻止插入重复的电子邮件条目。 我尝试过以下方法:
//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;
}
}
答案 0 :(得分:2)
两个问题:
checkEmail()
函数,因此它永远不会运行。您应该从函数中删除该代码,或者只调用需要运行的函数。where('email','=',"$email")
代替。答案 1 :(得分:0)
将mysql_result更改为mysql_num_rows,如第一个函数所示,然后尝试。
$result = mysql_num_rows(mysql_query($sql),0) ;
答案 2 :(得分: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;
}
}
还有几点:
答案 3 :(得分:0)
在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
}