2013-06-04 48 views
1

我想添加表单验证,如果ip_address重复只是 留在当前页面或给我看任何类型的消息。需要添加表单验证codeigniter

这是模式代码

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; 
    } 

这是CONTROLER

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'); 
     } 
    } 

听到的话,我添加新的投票是显示我的数据库错误的列是重复的,所以我不要我不想表明,如果它重定向到任何其他页面将会很好,如果留在当前页面仍然可用或弹出消息。

+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"; 
       } 
    } 

在你的控制器处理响应并相应地显示

+0

亲爱的约翰布雷克,我必须将它添加到我的模型为u有编辑在这里,但它在做同样的因为它是..在这里,当我插入它让我到数据库错误,所以我不''希望用户应该看到,只有当它被重复只是重定向到一个页面或任何按摩。谢谢 –

+1

当你插入重复的时候,Duplicate是否与数据库错误一起返回? –

+0

但你知道我想要在我的控制器或模型上,如果它是重复的,所以只要给我一个按摩,如表单验证。 –