dongzhen4180 2014-07-10 02:46
浏览 80

重定向不在codeigniter中工作

So here's what I want to do. I want to check if the userid in segment(3) exist or else it will redirect somewhere instead of still loading the view with an error.

Here's the example url

 http://localhost/ems/edit_user/edit_user_main/1001

Now if I try to edit the userid in segment(3) and intentionally put an invalid userid, it still loads the view and i don't know why

Here's my function

public function edit_user_main(){
    $id = $this->uri->segment(3);
    $check = $this->get_data->check_if_exist($id);
    if($check) {
        $data['title'] = 'Edit User';
        $data['id'] = $this->session->userdata('usertoedit');
        $this->load->model('accounts/get_data');   
        $item = $this->get_data->get_user($id);
        $data['user'] = $item[0];
        $data['main_content'] = 'edit_user/edit_user_main';
        $this->load->view('includes/template', $data);
    } else {
        redirect('admin/adminuser');
    }
}

Here's the model

public function check_if_exist($id){
    $query = $this->db->get_where('accounts',array('user_id'=>$id));
    if($query) {
        return TRUE;
    } else {
        return FALSE;
    }
}

There is no problem with the fetching of data. The problem is even if the userid doesn't exist, the view is still loading but with an error coz there's no data for that userID. It's not redirecting,

I tried using print_r and it working fine, the value of the $check is 1 when there's a valid userID.

Hope someone can help me with this. Thank you

  • 写回答

2条回答 默认 最新

  • doubei5310 2014-07-10 05:31
    关注

    With your function it will always return true because the statement

    $this->db->get_where('accounts',array('user_id'=>$id));

    will always execute,So you need to check query is returning any result row or not with the statement

    $query->num_rows().

    public function check_if_exist($id){
        $query = $this->db->get_where('accounts',array('user_id'=>$id));
        if($query->num_rows() > 0){ //change made here
           return TRUE;
        }
        else{
           return FALSE;
       }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大