-2

在我的管理员页面中,我有用户列表..我希望如果我单击用户名,它会将我重定向到其个人资料..这是我的管理员页面视图:

<table class="table table-striped table-bordered table-condensed">
<tr>
 <th>username</th>
 <th>firstname</th>
     <th>lastname</th>
 <th>email</th>
 <th>usertype</th>
 <th>Action</th>
</tr>
 <?php foreach ($info as $infos): ?>
<tr>
 <td>
  <?php 
   $user=$infos['username']; 
   print_r ($user);
   $this->session->set_userdata($user);
  ?>
  </td>
  <td><?php echo $infos['firstname']?></td>
  <td><?php echo $infos['lastname']?></td>
  <td><?php echo $infos['email']?></td>
 <td><a href="<?php echo base_url()?>users/showuser">Show</a> | <a href="<?php echo      base_url()?>users/deleteuser">Delete</a></td>
</tr>
<?php endforeach ?>

我的控制器的一部分是这样的:

  public function showuser()
{
    $this->load->helper(array('form','url'));
    $this->load->library('form_validation');
    $this->check_isValidated();

    $data['info'] = $this->users_model->user_info();
    $this->load->view('users/showuser',$data);          
}

在我的模型中:

public function user_info()
{
    $this->load->database();
    $this->db->select('username,firstname,lastname,email');
    $user = $this->session->userdata('user');
    $this->db->where('username',$user);
    $query = $this->db->get('users');
    if($query->num_rows > 0)
    {
        $row = $query->row();
        $data = array(
            'firstname' =>$row->firstname,
            'lastname' =>$row->lastname,
            'username' =>$row->username,
            'email' =>$row->email,
        );
        return $data;
    }else{
        return false;
    }

我的问题是,当我单击某个用户时,它不会显示其各自的个人资料,而是会显示您数据库中列出的第一个用户的个人资料。
如何比较我的模型中的 id

4

1 回答 1

1

你真的需要花一些时间从代码的外观上阅读一些教程,因为如果你继续这样下去,你会遇到很多问题。首先也是最重要的是您通过用户名而不是唯一 ID 加载用户,这意味着我猜测您的用户表甚至没有唯一 ID,这反过来意味着它没有被索引,因此最终会遇到性能问题。

无论如何,除了这个问题。第一个问题是观点。您没有为您想要的用户传递任何参数,因此您每次都只是加载一个没有任何参数的函数。您的 URL 应该如下所示,允许您每次都传递正确的参数。

//Again, you really should have a userId that you are passing here.
<a href="<?php echo base_url()?>users/showuser/$infos['username']">Show</a>

然后在你有问题二的模型中,你将登录用户的用户名传递给数据库,这样你就永远不会得到其他人的个人资料。见下文:

控制器:

//this sets the user to the parameter we added to the url above and passes it to the model function.
$data['info'] = $this->users_model->user_info($this->uri->segment(3));

模型:

public function user_info($user)
{
// you're now getting the user from the controller like you should be.
$this->load->database();
$this->db->select('username,firstname,lastname,email');
$this->db->where('username',$user);

我没有包括你所有的代码,只是相关的部分。请记住,虽然这将解决您的直接问题,但它不会解决您所做的其他问题。没有安全检查意味着任何拥有该 URL 的人都可以看到其他人的个人资料,似乎没有 ID 等。祝你好运,但确实需要一些时间并尽可能多地阅读有关数据库结构和了解 CI 工作原理的信息。

于 2013-01-18T12:41:41.047 回答