Check if a user registered with the bank has already registered cpf
Hello, guys I'm needing 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 application. I'll send 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 because I could not finish, I want to return the data of the user who is logged in.
2 answers
Using form_validation
in your Controller
it is possible to validate if a value is unique, informing 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
You can do as follows:
// 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.