0
我想獲得歡迎管理網站(crud)的會話fname,對於會話用戶名我已經擁有它,因爲它已經解密$_SESSION['username'] = $this->input->post('username');
,我的問題如何從數據庫創建會話fname?如何在codeigniter中檢索數據而無需發佈?
數據庫SQL
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`password` varchar(32) NOT NULL,
`fname` varchar(30) DEFAULT NULL,
`lname` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
)
控制器
<?php if(!defined('BASEPATH')) exit('Tidak ada akses!');
class C_login extends CI_Controller{
function __construct(){
session_start();
parent::__construct();
$this->load->model('m_user');
}
public function index(){
if(isset($_SESSION['username'])){
redirect('crud');
}
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == TRUE){
$this->load->model('m_user');
$result = $this->m_user->cek_login(
$this->input->post('username'),
$this->input->post('password')
);
if($result == TRUE){
$_SESSION['username'] = $this->input->post('username');
redirect('crud');
}
}
$this->load->view('login/v_form');
}
public function daftar(){
$data = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password)')),
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname')
);
$this->m_user->tambah_user($data);
$this->load->view('login/v_daftar',$data);
}
public function logout(){
session_destroy();
$this->index();
}
}
型號
<?php if(!defined('BASEPATH')) exit('Tidak ada access!');
class M_user extends CI_Model{
function __construct(){
parent::__construct();
}
function cek_login($username,$password){
$query = $this->db->where('username',$username)
->where('password',md5($password))
->limit(1)
->get('users');
if ($query->num_rows() > 0){
return $query->row_array();
}
else{
return FALSE;
}
}
function tambah_user($data){
return $this->db->insert('users', $data);
}
}
thx安東它的作品,我是codeigniter順便新手,如果你有任何鏈接教程也許可以幫助:D – Niinii 2013-04-18 06:29:40
我假設你正在關注視頻教程(這很好)。嘗試處理模型中的數據,然後將數組發送到控制器,然後發送到視圖。不要使用Objects,像上面的例子那樣使用數組。閱讀CI參考。寫得很好。 – Ando 2013-04-18 16:12:15