douyan2680 2013-06-04 03:51
浏览 59

需要添加表单验证codeigniter

I want to add form validation if the ip_address duplicated just stay on current page or show me any kind of message.

this is the model code

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

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

hear if i add new vote it's shows me database error as the column is duplicate so i don't want to show that, if it redirect me to any other page will be great if stay on current page still ok or any pop up message.

  • 写回答

1条回答 默认 最新

  • dongmangwei3822 2013-06-04 05:11
    关注

    Do a check before inserting into database.

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

    In your controller handle the response and display accordingly

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP