2015-12-26 63 views
0

我想验证我在codeigniter中的插入数据验证插入codeigniter

问题是代码返回重复条目的页面错误。我想把错误信息丢到主页。

这里是我的代码:

$data = array(
      'heheId' => $this->input->post('heheId'), 
      'userId' => $this->input->post('userId') 
     ); 
    $this->db->insert('tentarasaya',$data); 
    if ($this->db->affected_rows() > 0){ 
     $this->session->set_flashdata('info', "Hore sukses"); 
    } else { 
     $this->session->set_flashdata('danger', "Fail insert"); 
    } 
    redirect('my_home'); 

任何答案吗?

更新: 重复这样

enter image description here

+0

对于重复条目捆绑使用唯一的验证'$这 - > form_validation-> set_rules( 'FIELD_NAME', '场', '修剪|所需| is_unique [TABLENAME.COLUMNNAME]'); ' – Saty

+0

这是外键问题 –

+0

,并且您已在数据库中将其设置为** Unique **。 ***删除它*** –

回答

1

进入试试这个

$data = array(
    'heheId' => $this->input->post('heheId'), 
    'userId' => $this->input->post('userId') 
); 
if (!$this->db->insert('tentarasaya',$data)) { # add if here 
    # Unsuccessful 
    $this->session->set_flashdata('danger', "Fail insert"); 
} 
else{ 
    # Success 
    $this->session->set_flashdata('info', "Hore sukses"); 
} 
redirect('my_home'); 
+0

不工作,我仍然卡在重复条目的页面 –

+0

重复条目页面的意思?? –

+0

@Andhika检查[住宿评论](http://stackoverflow.com/questions/34471990/validating-insert-codeigniter#comment56685328_34471990) –

0

确保自动增量选项,在数据库中选择。我假设这里的user_id是一个主键,不需要自己将其插入到数据库中。

data = array(
      'heheId' => $this->input->post('heheId'), 
     ); 
    $this->db->insert('tentarasaya',$data); 

否则,如果要插入user_ids自己,你可以1)自定义一个回调函数来检查,看看是否在指定的用户ID在数据库中已存在,2)使用is_unique表单验证规则来与笨