Check if a registered user in the bank has already registered Cpf

Asked

Viewed 173 times

0

Hello, guys I need to check in the bank if a logged in user already has registered Cpf and return if it is false or not.

I use codeigniter in the app. I’ll send you the codes. CONTROLLER:

public function check_cpf_doctor() { 
$check_result = $this->Doctor_model->checkCPFDoctor($_POST['doctor_id']);
  print json_encode($check_result);
}

// DOCTOR_MODEL

public function checkCPFDoctor($id)
{
    $key = $this->config->item('encryption_key');

    $query = 

    if($query['crm'])

    {
        return true;
    }
    else
    {
        return false;
    }


}

this model query is wrong and incomplete.

  • You have a button to check or you are arriving in Ajax?

2 answers

1

Using form_validation in his Controller it is possible to validate if a value is unique, stating the table and searched value:

$this->form_validation->set_rules('cpf', 'Cpf', 'required|is_unique[doctors.cpf]');

To catch the errors just use the call validation_errors();

NOTE: exchange Doctors for the name of your table, if this is not its name.

https://www.codeigniter.com/userguide3/libraries/form_validation.html

0

You can do it this way:

// Controller
public function check_cpf_doctor() { 
    $check_result = $this->Doctor_model->checkCPFDoctor($this->input->post('doctor_id'));
    if($check_result==1){
        echo "Este CPF já está cadastrado";
    } else {
        echo "CPF não cadastrado";
    }
}

// Model
public function checkCPFDoctor(){
    $this->db->where('cpf', $this->input->post('doctor_id'));
    $check = $this->db->get('table')->num_rows();
    return $check;
}

In this case, we return with num_rows(), in case of return 1, it is already registered, if not, it is not registered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.