Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display prompt after found a duplicate data inside the database in codeigniter

need some help regarding about checking if data already exists in the database and system will display a prompt. So this is the code:

In model:

public function check_name($str, $col)

    {
    $lname = $this->input->post('lastname');
    $fname = $this->input->post('firstname');

        $result = $this->db->get_where('patients', array(
            'firstname' => $lname ,
            'lastname' => $fname
            ));
        if($result->num_rows() > 0){
             // $this->form_validation->set_message('check_name', 'The %s field already exists.');
            echo "Patient with the same name already exists.";

        }  else {
             return true;
        }
    }

What would be the proper way to call it inside my controller. Code above seems no working.

like image 315
claudios Avatar asked Aug 17 '26 13:08

claudios


1 Answers

You have two Parameters in your function which are not used anywhere. Try doing this:

public function check_name($firstname, $lastname)
{
    /*
    $firstname = $this->input->post('lastname');
    $lastname = $this->input->post('firstname');
    */

$query = $this->db->query("SELECT firstname, lastname FROM patients WHERE firstname = ".$firstname." AND lastname = ".$lastname." ");    

if($query->num_rows() == 0){
// $this->form_validation->set_message('check_name', 'The %s field already exists.');
echo "Patient with the same name already exists.";

}  else {
return true;
}
}
like image 76
Ilanus Avatar answered Aug 20 '26 03:08

Ilanus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!