我有一个包含复选框数组的表单,我试图检查数据库是否存在某个复选框值,如果没有则将其插入数据库:
这是我目前的代码:
查看页面:
<form action="<?php echo site_url('test/post_form')?>" method="post">
<input type="text" name="full_name">
<input type="checkbox" name="subject[]" value="Algebra" >
<input type="checkbox" name="subject[]" value="Trigonometry" >
<input type="checkbox" name="subject[]" value="Geometry" >
<input type="submit" name="submit">
控制器页面:
public function post_form()
{
$post_data = array(
'subject'=>$this->input->post('subject'),
'full_name'=>$this->input->post('full_name'),
);
$this->load->model('subject_model');
if($this->subject_model->check_subject($post_data)===true)
{
$this->session->set_flashdata('msg', 'Already Exist');
redirect('test/error_page');
}
else
{
insert to database
}
}
模特页面:
public function check_subject($post_data=array())
{
extract($post_data);
foreach($post_data as $key=>$value)
{
$this->db->select('subject');
$this->db->where('subject',$value['subject']);
$this->db->where('full_name',$value['full_name']);
$this->db->from('subject_table');
$query=$this->db->get();
if ($query->num_rows() > 0)
{
return true;
}
else
{
return true;
}
}
示例场景:
答案 0 :(得分:0)
我认为你必须在单独的数组中跟踪数据库中找到的主题并返回结果,它可能看起来像你可以将它设置为True FALSE
组合或任何其他适合你的东西
public function check_subject($post_data=array())
{
extract($post_data);
$subjects=array();
foreach($post_data as $key=>$value)
{
$this->db->select('subject');
$this->db->where('subject',$value['subject']);
$this->db->where('full_name',$value['full_name']);
$this->db->from('subject_table');
$query=$this->db->get();
if ($query->num_rows() > 0)
$subjects[$value['subject']]=true;
else
$subjects[$value['subject']]=FALSE;
}
return $subjects;
}
答案 1 :(得分:0)
实际上你的主题是作为数组发布的,所以你这样检查
public function check_subject($post_data=array())
{
$val = " '". implode( "', '",$post_data['subject']) ."' " ;
$this->db->select('subject');
$this->db->where(" subject in {$val} ");
$this->db->where('full_name',$value['full_name']);
$this->db->from('subject_table');
$query=$this->db->get();
if ($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
检查它我认为它会起作用