Check if email already exists in database

后端 未结 4 1508
萌比男神i
萌比男神i 2021-01-29 04:28

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 d         


        
4条回答
  •  -上瘾入骨i
    2021-01-29 04:58

    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

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

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

提交回复
热议问题