需要添加表单验证codeigniter

时间:2013-06-04 03:51:15

标签: php mysql codeigniter

如果ip_address只是重复,我想添加表单验证 留在当前页面或向我显示任何类型的消息。

这是模型代码

  public function add_vote($option_id)
    {
        $this->db->insert($this->votes_table, array(
            'option_id' => $option_id,
            'ip_address' => $this->input->ip_address(),
            'timestamp' => time()
        ));

        return ($this->db->affected_rows() == 1) ? TRUE : FALSE;
    }

这是控制器

public function vote($poll_id, $option_id)
    {
        if ( ! $this->poll_lib->vote($poll_id, $option_id))
        {
            $data['base_styles'] = 'public_html/res/css/base.css';
            $data['title'] = 'Sorry an error occured';
            $data['error_message'] = $this->poll_lib->get_errors();
            $this->load->view('header');
            $this->load->view('www/posts/postclose', $data);
        }
        else
        {
            redirect('posts', 'refresh');
        }
    }

听说我是否添加了新的投票它显示了我的数据库错误,因为该列是重复的所以我不想显示,如果它将我重定向到任何其他页面将是伟大的如果留在当前页面仍然可以或任何流行消息。

1 个答案:

答案 0 :(得分:0)

在插入数据库之前进行检查。

public function add_vote($option_id)
    {   
        $query = $this->db->get_where($this->votes_table, array('ip_address' => $this->input->ip_address()));

        if($query->num_rows() < 1)

        {    $this->db->insert($this->votes_table, array(
                'option_id' => $option_id,
                'ip_address' => $this->input->ip_address(),
                'timestamp' => time()
             ));

             return "Successfully Inserted";
        }else{
               return "Duplicate";
              } 
    }

在您的控制器中处理响应并相应显示

相关问题