[英]How to get the id from the url then insert it into the data base using codeigniter
希望所有这些信息有所帮助。
所以我正在做的是我有一个我设置的论坛页面你可以选择一个类别,从该类别你可以在该类别中插入一个帖子。 我需要帮助的是获取数据库的该类别的ID,以便当我回显它时将显示帖子。 换句话说,在插入时将id链接到页面。
好吧所以我知道它插入了用户名消息标题ect但它没有做的是从url获取1并将该1插入category_id下的数据库
这是我的网址,我省略了主要的http来缩短它,但其余的是数字1是我想要得到的并插入原因,它会根据你选择的类别而改变。 index.php/forum/create_post/1
这是我的类别表
ID title
1 community
这是我的帖子表所有的主要信息来自我想要将category_id连接到主类别表。
id , 标题消息日期用户id category_id flaged用户名
这是将插入新帖子的第一个视图
视图:
<div id="container">
<div class="module">
<?php echo form_open('forum/create_post'); ?>
<div>
<?php
echo form_label('Title', 'title');
echo form_input('title', '');
?>
</div>
<div>
<?php
echo form_label('Message', 'message');
echo form_input('message', '');
?>
</div>
<div>
<?php echo form_submit('create', 'create new post'); ?>
</div>
<?php echo form_close(); ?>
</div>
</div>
控制器:这是控制器中的部分,如果没有,我正在传递所有输入
public function create_post() {
if( !$this->session->userdata('username') ) {
redirect('/'); // please login to continue
}
if( $this->input->post('create') ) {
$this->forum->createpost(array(
// $id= $this->uri->segment(3),
'title' => $this->input->post('title'),
'message' => $this->input->post('message'),
'user_id'=> $this->session->userdata('id'),
'username'=>$this->session->userdata('username'),
));
}
$this->load->view('templates/header');
$this->load->view('forum/create_post');
$this->load->view('templates/footer');
}
这是我要插入数据的模型
public function createpost($data){
$this->db->insert('post',$data);
}
根据您的URL index.php/forum/create_post/1
,您的控制器功能应如下所示,以符合CI标准。
public function create_post($category_id) {
所以你可以直接访问$category_id
。 无需获取网址段。
$res = $this->forum->createpost(array(
$id= $category_id,
'title' => $this->input->post('title'),
'message' => $this->input->post('message'),
'user_id'=> $this->session->userdata('id'),
'username'=>$this->session->userdata('username'),
));
if($res)
{
// show thanks msg
}
else
{
// show error msg
}
在你的模型中:
您可以检查是否已插入数据
public function createpost($data)
{
$this->db->insert('post',$data);
if($this->db->affected_rows()>0)
return true;
else
return false;
}
您没有再次向页面发送/ 1。 您的表单必须从原始URL发送/ 1。
<?php echo form_open('forum/create_post'); ?>
create_post方法不会收到第3段,因为它不存在。 将类别ID存储在隐藏的输入中,并通过$ this-> input-> post('category_id')访问它:
public function create_post() {
if( !$this->session->userdata('username') ) {
redirect('/'); // please login to continue
}
if( $this->input->post('create') ) {
$this->forum->createpost(array(
'category_id' => $this->input->post('category_id'),
'title' => $this->input->post('title'),
'message' => $this->input->post('message'),
'user_id' => $this->session->userdata('id'),
'username'=>$this->session->userdata('username')
));
}
$data['category_id'] = $this->uri->segment(3);
$this->load->view('templates/header');
$this->load->view('forum/create_post', $data);
$this->load->view('templates/footer');
}
<div id="container">
<div class="module">
<?php echo form_open('forum/create_post'); ?>
<div>
<?php
echo form_label('Title', 'title');
echo form_input('title', '');
?>
</div>
<div>
<?php
echo form_label('Message', 'message');
echo form_input('message', '');
?>
</div>
<div>
<?php echo form_submit('create', 'create new post'); ?>
</div>
<input type="hidden" name="category_id" value="<?php echo $category_id; ?>">
<?php echo form_close(); ?>
</div>
</div>
另外,问问自己是否需要存储用户ID和用户名。 用户名是否附加了ID? 仅通过从帖子中选择userID就无法检索到它?
我发现了我需要做的所有工作我必须将uri段插入隐藏的输入论坛,以便将其插入到正确的id框中,现在我可以连接正确的类别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.