Check if a user registered at the bank has cpf already registered

0

Hello, I have to check in the database if a logged in user already has cpf registered and return whether it is false or not.

I use the 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 it, I want it to return the data of the user that is logged in.

    
asked by anonymous 01.12.2018 / 23:28

2 answers

1

By using form_validation in your Controller it is possible to validate if a value is unique, informing the table and value searched:

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

To catch the errors just use the validation_errors();

NOTE: Change doctors by the name of your table if this is not their name.

  

link

    
03.12.2018 / 16:48
0

You can do this 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.

    
02.12.2018 / 22:36