douduxia1551 2014-09-10 15:59
浏览 31
已采纳

使用codeigniter进行更新

I finished the tutorial for making a news application on the codeigniter user_guide. I am now trying to extend the tutorial website so it loads text into a textarea that can be retyped and used to update the entry. I want the view article page to be this same page for updating. The view loads correctly, but when I click submit, it brings me from localhost/ci/news/foo to localhost/ci/news/view along with a 404. I have mostly tried to imitate the create, and only altering the model query, but since I've been struggling with it for a while, have tried several ideas.

I suspect the problem could be one of the following.

Something directing me to ci/news/view Misusing my $slug variable Misconnecting the controller and model Syntax in my model query.

I'll post all the relevent code.

Here's my controller function for the view, which is also where updates will happen.

public function view($slug)
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['news_item'] = $this->news_model->get_news($slug);

    $slug = $slug;

    $this->form_validation->set_rules('text', 'text', 'required');

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

    $data['title'] = $data['news_item']['title'];
    if ($this->form_validation->run() === FALSE)
    {
    $this->load->view('templates/header', $data);
    $this->load->view('news/view', $data);
    $this->load->view('templates/footer');
    }
    else
    {
        $this->news_model->update_news();
        $this->load->view('news/success');
    }
}

Here's my update_news() in the model

public function update_news($slug)
{
$data = array(
    'slug' => $slug,
    'text' => $this->input->post('text')
);
$this->db->set('slug', $slug);
$this->db->update('news', $data);
}

and my view.php

<?php 
echo validation_errors(); 
echo form_open('news/view'); 

echo '<h2>'.$news_item['title'].'</h2>';
?>

<label for="text">Text</label>
<textarea name="text"><?php echo $news_item['text']; ?></textarea><br />

<input type="submit" name="submit" value="Update" />
</form>

UPDATE: as requested, my get_news model function.

public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
    $query = $this->db->get('news');
    return $query->result_array();
}

$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
  • 写回答

3条回答 默认 最新

  • doushaizhen1244 2017-01-08 19:43
    关注

    In your model just put where instead of set.

     public function update_news($slug)
    {
    $data = array(
        'slug' => $slug,
        'text' => $this->input->post('text')
    );
    $this->db->where('slug', $slug);
    $this->db->update('news', $data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码