【问题标题】:Validating insert codeigniter验证插入代码点火器
【发布时间】:2015-12-26 13:55:36
【问题描述】:

我正在尝试在 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');

有答案吗?

更新: 像这样重复输入

【问题讨论】:

  • 重复输入使用唯一验证$this->form_validation->set_rules('field_name', 'Field', 'trim|required|is_unique[TABLENAME.COLUMNNAME]');
  • 这是外键问题
  • 并且您已在数据库中将其设置为 Unique删除它

标签: php mysql codeigniter activerecord mysqli


【解决方案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');

【讨论】:

  • 不工作,我仍然卡在重复条目的页面上
  • 重复进入页面是什么意思??
  • @Andhika 检查Stays comment
【解决方案2】:

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

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

否则,如果您希望自己插入 user_ids,您可以 1) 定义自定义回调函数来检查指定的用户 id 是否已存在于数据库中,2) 使用与 codeigniter 捆绑在一起的 is_unique 表单验证规则

【讨论】:

    猜你喜欢
    • 2011-10-25
    • 2011-05-30
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多