Codeigniter - 表单验证不起作用

时间:2017-04-06 09:21:50

标签: forms codeigniter validation

我已经尝试使用Google搜索但未解决,我的代码是在插入之前检查重复,当" tagnumber"字段已存在将转到特定页面而不插入表,它可以工作,但问题是虽然没有插入表但它不会去我想要的特定页面。

下面是条件语句的问题:
如果数据存在,表单验证将执行不插入数据 如果数据不存在表单验证将执行插入数据但转到错误页面(与存在数据页面相同的页面)。

我的控制者:

function tambahSubmit()
{

$tagnumber = $this->input->post("tagnumber");
$this->myigniter_model->addData($tagnumber); 
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');

// field name, error message, validation rules
$this->form_validation->set_rules('tagnumber', 'tagnumber', 'trim|required|is_unique[inventorytag.tagnumber]');
$this->form_validation->set_rules('date', 'date', 'trim|required');
$this->form_validation->set_rules('employee', 'employee', 'trim|required');
$this->form_validation->set_rules('semnumber', 'semnumber', 'trim|required');
$this->form_validation->set_rules('quantity', 'quantity', 'required');
$this->form_validation->set_rules('area', 'area', 'trim|required');



if($this->form_validation->run() == false)
{

    $this->load->view('YearEndStock/tampilan_input_gagal');
}
else
{

    $this->myigniter_model->addData($tagnumber);

    $this->load->view('YearEndStock/tampilan_input_sukses');


}
}

我的模特:

function addData($tagnumber)
{
  // Added $this->db->escape() and limit 1 for Performance
$query = $this->db->query("SELECT tagnumber FROM inventorytag WHERE tagnumber = ".$this->db->escape($tagnumber)." limit 1");

$data = array(      
'tagnumber' => $this->input->post('tagnumber'), 
'date'=> date('Y-m-d H:i:s'),
'employee' => $this->input->post('employee'),
'semnumber' => $this->input->post('semnumber'), 
'quantity' => $this->input->post('quantity'), 
'area' => $this->input->post('area') 
);

return $query->num_rows() == 0 ? $this->db->insert('inventorytag', $data) : false;
}

1 个答案:

答案 0 :(得分:0)

然后你只需要分开你的真实陈述。删除你的is_unique验证。

 <a href="@Url.Action("About", "home", new { controller  ="Hello" , test ="Amit"})">Check</a>