dongtan3306 2017-04-24 07:42
浏览 38

更新数据库[codeIgniter]

I'm new to codeIgniter, I have followed the main tutorial and now I'm trying to create the update funtion, but it gives to me an error that I don't know why.

Here is my model

public function update_news($id=0)
    {
        $this->load->helper('url');
        $slug = url_title($this->input->post('title'),'dash',TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'text' => $this->input->post('text')
        );

        $this->db->where('id', $id);

        return $this->db->update('news', $data);
    }

Controller:

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

        $data['title'] = 'Editar noticia';

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

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('news/update');
            $this->load->view('templates/footer');

        }
        else
        {

            $this->news_model->update_news($id);
            $this->load->view('news/success');
        }
    }

View

<?php
  function showComplete()
  {
    echo site_url('news/'.$news_item['slug']);
  }

?>
<?php foreach ($news as $news_item): ?>

<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
  <?php echo $news_item['text']; ?>
</div>

 <p>
    <a href="<?php echo site_url('news/'.$news_item['slug']); ?>" class="btn btn-primary">Ver Noticia</a>
    <a href="<?php echo site_url('news/update/'.$news_item['id']); ?>" class="btn btn-warning">Actualizar</a>
    <a href="<?php echo site_url('news/delete/'.$news_item['id']); ?>" class="btn btn-danger">Borrar</a>
 </p> 

<?php endforeach; ?>

    <a href="<?php echo site_url('news/create'); ?>" class="btn btn-default"> Nueva noticia</a>

------edit-------

Now I do not have errors, thanks guys, but now I think that my form_open from my update page is missing the variable passing, how can I make that the form pass the ID to my model?

update view

<h2><?php echo $title; ?></h2>

<?php echo validation_errors(); ?>

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

    <div class="col-md-8">
        <label for="title">Title</label>
        <input type="input" name="title" class="form-control"> <br>

        <label for="text">Text</label>
        <textarea  class="form-control" name="text" id="" cols="30" rows="10"></textarea> <br>

        <input class="btn btn-primary" type="submit" name="submit" value="Create new item">
    </div>

</form>
  • 写回答

6条回答 默认 最新

  • dongsha1544 2017-04-24 07:50
    关注

    you have to pass the data of your input to update news model , Always follow the MVC structure . because code igniter is MVC Framework.

    public function update($id)
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
    
        $data['title'] = 'Editar noticia';
    
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');
    
        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('news/update');
            $this->load->view('templates/footer');
    
        }
        else
        {
            $slug = url_title($this->input->post('title'),'dash',TRUE); // i dont know about this slug. that you were using for what?
            $data = array(
               'title' => $this->input->post('title'),
               'text' => $this->input->post('text')
            );
            $this->news_model->update_news($id,$data);
            $this->load->view('news/success');
        }
    }
    

    and your model function will look like this

    public function update_news($id,$data)
    {
        $this->db->where('id', $id);
        return $this->db->update('news', $data);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看