php - 在 codeigniter 中发现数据库中有重复数据后显示提示

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

在我的 Controller 中调用它的正确方法是什么。上面的代码似乎不起作用。

最佳答案

您的函数中有两个未在任何地方使用的参数。尝试这样做:

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

关于php - 在 codeigniter 中发现数据库中有重复数据后显示提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33296398/

相关文章:

php - 根据归一化保存文章及相关分类

php - 如何在 Codeigniter 中获取特定月份的天数?

apache - 强制 Codeigniter HMVC 使用 SSL

php - 在 zend 框架中使用 union 进行 sql 查询

php - 获取多文件上传文件的名称

php - 去掉括号前后的空格

php - 如何使用河豚散列长密码(> 72 个字符)

PHP 代码不工作,没有错误

php - Codeigniter POST/GET 变量和 $this->input->post

php - 使用 codeigniter php 在 mysql 中添加日期时间