在codeigniter中找到数据库内的重复数据后显示提示

时间:2015-10-23 06:51:56

标签: php codeigniter

需要一些关于检查数据是否已存在于数据库中的帮助,系统将显示提示。所以这是代码:

模特:

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;
        }
    }

在我的控制器内调用它的正确方法是什么。上面的代码似乎没有用。

2 个答案:

答案 0 :(得分:1)

将您的代码更改为:

public function check_name($str, $col)

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

    $this->db->where_in('patients', array(
        'firstname' => $lname ,
        'lastname' => $fname
        ));
    $result = $this->db->get('your_table_name');
    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.";
        return false;

    }  else {
         return true;
    }
}

未使用2个变量 $ str $ col 。如有必要,删除。 如果有效。告诉我。

答案 1 :(得分:1)

您的函数中有两个参数,不会在任何地方使用。试着这样做:

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;
}
}