此程序将让您先登录,然后我需要做的就是编辑他们的个人资料/信息。当我点击配置文件的页面并且没有显示任何内容时,我无法从数据库中获取数据。
MODEL
function login($username, $password) {
$this->db->select('username, password');
$this->db->from('tblsec');
$this->db->where('username', $username);
$this->db->where('password', MD5($password));
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
public function get_id($username) {
$query = $this->db->query("SELECT * from tblsec WHERE username = '$username'");
$r = $query->result();
return $r;
}
查看
<?php foreach ($username as $user): ?>
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-5">
<?php echo $user->firstname; ?> <?php echo $user->lastname; ?>
</div>
</div>
<br>
<div class="form-group">
<label class="col-sm-2 control-label">Password</label>
<div class="col-sm-5">
**********
</div>
</div>
<br>
<div class="form-group">
<label class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-5">
<?php echo $user->lastname; ?>
</div>
</div>
<br>
<br>
<div class="form-group">
<label class="col-sm-2 control-label">Email</label>
<div class="col-sm-5">
<?php echo $user->email; ?>
</div>
</div>
<br>
<?php endforeach; ?>
</div>
</div>
CONTROLLER
function __construct() {
parent::__construct();
$this->load->model('secretary_model', '', TRUE);
$this->load->library('form_validation');
$this->load->helper('date');
}
public function index() {
if ($this->session->userdata('logged_in')) {
$this->header();
$this->load->view('secretary/sec_login_view');
} else {
redirect('secretary/sec_login_view');
}
}
public function profile() {
if ($this->session->userdata('logged_in')) {
$username = $this->session->userdata('username');
$data['username'] = $this->secretary_model->get_ID($username);
$this->header2();
$this->load->view('secretary/secretary_profile', $data);
} else {
redirect('secretary/login', 'refresh');
}
}
答案 0 :(得分:1)
首先,确保已设置logged_in
会话,以确保用户已登录。像
// After checking the username and password
$this->session->set_userdata('logged_in', true);
第二,如果未找到用户名或相关数据,则返回模型中的错误或相应错误。
/**
*@return mixed object|false
*/
function get_id($username) {
$query = $this->db->query("SELECT * from tblsec WHERE username = '$username'");
return $query->num_rows() > 0 ? $query->result() : false;
}
然后在您的视图中检查您的视图值,如下所示:
if( is_array($username) && count($username) > 0 ) {
foreach ($username as $user):
// Set data accordingly
endforeach;
}
else {
echo "Sorry! Related Data not found!";
}
这是一种相应的获取和调试方法的好方法。