为什么数据库不更新Codeigniter

时间:2018-10-30 22:41:55

标签: php database codeigniter

我目前正在使用此php应用程序,该应用程序中有一个论坛,我需要编辑特定主题的功能。

我的模特:

public function update_topic(){

        $data = array(
            'topic_subject' => $this->input->post('topic_subject'),
            'body' => $this->input->post('body'),
            'topic_cat' => $this->input->post('topic_cat'),
            'topic_by' => $this->session->userdata('user_id')

        );

        $this->db->where('topic_id', $this->input->post('topic_id'));
        return $this->db->update('topics', $data);

    }

我的控制器:

    public function edit($topic_id){
    $data['topic'] = $this->topic_model->get_topics($topic_id);
    $data['forumcategories'] = $this->forumcategory_model->get_forumcategories();

    if(empty($data['topic'])){
        show_404();
    }

    $data['title'] = 'Edit Topic';

    $this->load->view('templates/header');
    $this->load->view('forum/topics/edit', $data);
    $this->load->view('templates/footer');
}

public function update(){

    if(!$this->session->userdata('logged_in')){
        redirect('users/login');
    }

    $this->topic_model->update_topic();

//  $this->session->set_flashdata('post_updated', 'Post has been updated');
    redirect('forumcategories');
}

我的表单:

    <h2><?= $title; ?></h2>

<?php echo validation_errors(); ?>


<?php echo form_open('topics/update'); ?>

  <div class="form-group">
    <label>Topic Subject</label>
    <input type="text" class="form-control" name="topic_subject" placeholder="Add Subject" value="<?php echo $topic['topic_subject']; ?>">
  </div>
  <div class="form-group">
    <label>Body</label>
    <textarea id="editor" class="form-control" name="body" placeholder="Add text"><?php echo $topic['body']; ?></textarea>
  </div>
   <div class="form-group">
  <label>Category</label>
  <select name="category_id" class="form-control">
  <?php foreach($forumcategories as $forumcategory): ?>
    <option value="<?php echo $forumcategory['cat_id']; ?>"> Name: <?php echo $forumcategory['cat_name']; ?> Description: <?php echo $forumcategory['cat_description']; ?></option>
  <?php endforeach; ?>
</select>
</div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

W

当我单击“编辑”按钮时,整个信息会完美加载,提交时不会引发任何错误,它会将我重定向到我想要的页面,但是信息未更新,这可能是问题所在吗? / p>

1 个答案:

答案 0 :(得分:0)

此代码进入控制器以进行更好的练习,您可以向我们展示@Alex所说的+调试形式还是最后一个查询的回声

 $dataa = array(
                'topic_subject' => $this->input->post('topic_subject'),
                'body' => $this->input->post('body'),
                'topic_cat' => $this->input->post('topic_cat'),
                'topic_by' => $this->session->userdata('user_id')

            );

然后使用$ data调用模型

$this->topic_model->update_topic($dataa);