在codeigniter

时间:2017-02-01 20:44:57

标签: php mysql codeigniter

$date['uid']            = $this->session->userdata('id');
//$date['uid']          = $this->input->post('uid');
$date['uname']          = $this->input->post('uname');
$data['title']          = $this->input->post('title');
$data['description']    = $this->input->post('desc');
$data['created']        = date('Y-m-d H:i:s');

我一直在尝试在表名为uid的新闻表中保存用户ID。我尝试过不同的方法,但徒劳无功

$date['uid']            = $this->session->userdata('id');

当我尝试使用session获取id时,它返回0.甚至当我尝试通过表单

发布uid时
 <input type="hidden" value=<?php echo $this->session->userdata('id');?>

OR

 <input type="hidden" value=<?php echo $_SESSION['id'];?>

它还将0值保存到数据库

1 个答案:

答案 0 :(得分:2)

你有没有试过这个..

application/config/autoload.php

中加载会话库
$autoload['libraries'] = array('session');

然后

$data = array(
            'uid' => $this->session->userdata('id');,
            'uname' => $this->input->post('uname'),
            'title' => $this->input->post('title'),
            'description' => $this->input->post('desc'),
            'created' => date('Y-m-d H:i:s')
            );

//print_r($data);
$this->db->insert('table_name',$data); //inserts into table