【问题标题】:Unable to fetch data from database in codeigniter php无法在codeigniter php中从数据库中获取数据
【发布时间】:2016-11-01 10:36:04
【问题描述】:

一旦用户登录到网站无法从数据库中获取数据,如果我在这里编写 foreach 条件,这是我的代码。获取用户名和登录验证工作正常。

控制器:

public function index()
{
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
}
else{

    $this->load->view('welcome');
}
}

型号:

function getprofiledata($id)
{
    $this->db->select('profile_details.*');     
    $this->db->from('profile_details');     
    $this->db->where(array('profile_details.profile_id'=>$id));     
    $q=$this->db->get();        
    if($q->num_rows()>0)
      {
    return $q->result();
        }
    else
    {
    return false;
    }
}

查看:

<div id="legend">
    <legend class="">Profile Information</legend>
</div>   
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
            <?php foreach($records as $r):?>
    <form action="<?php echo base_url();?>profile/updateprofile" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
    <?php
        echo form_hidden('profile_id',$r->profile_id);
    ?>
    <div class="form-group">
      <label class="control-label col-sm-2 " for="name">Name:</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2 " for="profilename">Profile Name:</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="profile_name" placeholder="Enter Profile name">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2 " for="designation">Designation:</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="designation" placeholder="Enter Designation">
      </div>
    </div>
    <button type="submit" class="btn">Submit</button>
    </form>
    <?php endforeach;endif;?>

【问题讨论】:

  • 对于初学者来说,我会在加载视图之前执行 var_dump($data) 并检查您所期望的内容。
  • @TimBrownlaw 不明白你在说什么
  • $records= $this-&gt;profile_model-&gt;getprofiledata($this-&gt;uri-&gt;segment(3)); $data['records']= $records; var_dump($record); 然后显示var_dump 结果
  • @razibalmamun 将错误作为未定义的变量记录并获得空白页
  • 您在哪里查看 var_dump ?并且您确定您在$this-&gt;uri-&gt;segment(3) 中获得了个人资料ID 吗?告诉我

标签: php codeigniter


【解决方案1】:

您在$data['records']= $this-&gt;profile_model-&gt;getprofiledata($this-&gt;uri-&gt;segment(3)); 行选择了错误的段号。

请注意,段计数从零开始,所以段 3 实际上是 uri 中的第 4 个段。

如果您将用户 ID 保留在会话中,则应将 $data['records']= $this-&gt;profile_model-&gt;getprofiledata($this-&gt;uri-&gt;segment(3)); 替换为 $records = $this-&gt;profile_model-&gt;getprofiledata($this-&gt;session-&gt;userdata('profile_id'))‌​‌​‌​;。你就完成了。

【讨论】:

    【解决方案2】:

    在登录模型中添加一个新会话,如下所示:

    <?php
    public function login_user($user_name = '', $password=''){ 
        $userdetails = array( 
        'email' => $user_name, 
        'password' => md5($password), 
        'status'=>1,     
        ); 
    
        $this->db->where($userdetails); 
        $query = $this->db->get('profile_details');  
    
        if($query->num_rows()): 
            $user = $query->result(); 
            $sess_arry = array( 
                'profile_id' => $user[0]->profile_id, // add new session profile_id
                'first_name' => $user[0]->first_name 
            );  
            $this->session->set_userdata('admin_logged_in', $sess_arry);    //add admin details to session   
            return true; 
        else: 
            return false; 
        endif; 
    }
    ?>
    

    还有一些更改您的 index 方法,如下所示:

    <?php
    public function index() 
    { 
        if($this->session->userdata('admin_logged_in')){ 
            $data['admin_details'] = $this->session->userdata('admin_logged_in'); 
            $data['country'] = $this->signup_model->getcountry();   
            $data['states'] = $this->profile_model->getstates();
    
            $profile_id = $this->session->userdata('profile_id');
            $records = $this->profile_model->getprofiledata($profile_id)‌​‌​;
    
            $data['records']= $records; 
    
            $data['mainpage']='profile'; 
            $this->load->view('templates/template',$data); 
            $this->load->view('templates/sidebar',$data); 
        } 
        else{ 
            $this->load->view('welcome'); 
        } 
    } 
    ?>
    

    我添加了新的 3 行,因为我认为您在 $this-&gt;uri-&gt;segment(3) 中没有正确获取个人资料 ID

    所以,

    $profile_id = $this->session->userdata('profile_id');
    $records = $this->profile_model->getprofiledata($profile_id)‌​‌​; 
    
    $data['records']= $records;
    

    【讨论】:

    • 我已经编辑了我的答案。 $records = $this-&gt;profile_model-&gt;getprofiledata($profile_id)‌​‌​;复制此行并粘贴您的页面,并告诉我
    猜你喜欢
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多