【问题标题】:How to get the id from the url then insert it into the data base using codeigniter如何从 url 获取 id 然后使用 codeigniter 将其插入数据库
【发布时间】:2014-01-21 06:14:57
【问题描述】:

希望所有这些信息对您有所帮助。

所以我正在做的是我设置了一个论坛页面,您可以选择一个类别,从该类别中您可以将帖子插入该类别。我需要帮助的是为数据库获取该类别的 ID,以便在我回显时显示该帖子。换句话说,在插入时将 id 链接到页面。

好的,所以我知道它插入了用户名消息标题等,但它没有做的是从 url 获取 1 并将该 1 插入到 category_id 下的数据库中

这是我的网址,我省略了主要的 http 以缩短它,但其余部分是数字 1 是我想要获取和插入的内容,因为它会根据您选择的类别而改变。 index.php/forum/create_post/1

这是我的类别表的内容

ID    title   

1     community

这就是我的帖子表的所有主要信息的来源,并且我想将 category_id 连接到主类别表。

idtitle消息日期用户id category_id 标记用户名

这是插入新帖子的第一个视图

查看:

<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);


}

【问题讨论】:

  • 您是否要传递主类别 id 来创建页面并作为父类别存储在 db 中?
  • 获取 url 参数 1 使用 $id= $this->uri->segment(3);
  • 好的,在它检索之前我已经完成了,但是我如何让它插入到数据库中?
  • 我想如果我理解你的话,对吗?我正在尝试 t0 做的是从类别中获取主 ID 并将其插入到 category_id 中,以便链接到其所在的类别。这样,当它被回显时,它只会在该类别中回显

标签: php codeigniter codeigniter-url


【解决方案1】:

根据您的 URL index.php/forum/create_post/1,您的控制器功能应如下所示以满足 CI 标准。

public function create_post($category_id) {

所以你可以直接访问$category_id。无需获取 url 段。

   $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;
}

【讨论】:

  • 所以一旦我这样做了,如何确保它输入到数据库中是在空白处?
  • 我无法理解您的问题。是否要确保数据已插入?
  • 是的,我想把所有的数据连同它所在的页面的 id 一起作为它被插入的类别,并将 id 和其他东西一起插入到数据库中
  • 完全希望如果不让我知道会更有意义
  • 好的,所以我知道它插入了用户名消息标题等,但它没有做的是从 url 获取 1 并将该 1 插入到 category_id 下的数据库中
【解决方案2】:

您不会再次向该页面发送 /1。您的表单必须从原始网址发送 /1。

<?php echo form_open('forum/create_post'); ?>

create_post 方法不会收到第三段,因为它不存在。将类别 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吗?不能通过从帖子中选择用户ID来检索它吗?

【讨论】:

    【解决方案3】:

    我发现我需要做些什么才能使这一切正常工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2020-07-23
      • 2017-02-06
      • 2021-09-15
      • 1970-01-01
      相关资源
      最近更新 更多