简体   繁体   中英

Check whether data already exist or not

I want to check either email, Identity card number or passport number has been registered before so, I use this query:

$qryc = "SELECT user_id FROM users WHERE login='".$this->input->post('email')."' 
        OR ic_number='".$this->input->post('ic_number')."' OR passport_number='".$this->input->post('passport_number')."'";
        $sqlc = mysql_query($qryc);
        $rsc = mysql_fetch_array($sqlc);
        $rowc = mysql_num_rows($sqlc);
        if($rowc > 0){

        redirect('registration/view_error/'.$rsc['user_id']);
        }else{
        $qryuser = "INSERT INTO users (login,password,user_type,ic_number,passport_number,user_application_status,verification_code)
                    VALUES ('".$email."',
                            '".$password_hashed."',
                            '2',
                            '".$ic_number."',
                            '".$passport_number."',
                            '0',
                            '".$verification_code."')";
        $sqluser = mysql_query($qryuser); 
        $userid = mysql_insert_id();

If the data already exist, it will bring error page to user. If not, there will be thank you page. The problem with my query is if the user never registered before, it still appear error page which tell them that the data already exist in database.

Otherwise, is there any other ways to check whether the data already exist in the database?

You are checking DB against email , ic_number or passport_number . Is there possibility that one of the fields is empty? Then it can match with some DB row where it is also empty and you got records. You should check values to empty before you compose your query.

You should change your query to look like this:

$icNumber = $this->input->post('ic_number');
$passportNumber = $this->input->post('passport_number');

$qryc = "SELECT user_id FROM users WHERE login='".$this->input->post('email')."'";

if ((isset($icNumber) && trim($icNumber) != ''))
{
$qryc .= " OR ic_number='".$icNumber."'";
}

if ((isset($passportNumber) && trim($passportNumber) != ''))
{
$qryc .= " OR passport_number='".$passportNumber."'";
}

确保所有字段(电子邮件,身份证号码或护照号码)均包含值,也可以尝试打印$ rsc以查看是否返回任何行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM