需要一些帮助,关于检查数据是否已经在数据库中存在,系统将显示提示。这是代码:
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;
}
}
在我的控制器中调用它的合适方式是什么?上面的代码似乎不起作用
你的函数中有两个参数没有在任何地方使用。试试这样做:
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;
}
}
把你的代码改成这样:
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;
}
}
两个变量$str和$col未使用。如果不需要就去掉。如果有效的话。告诉我。