2017-04-24 107 views
1

我是新来的codeIgniter,我跟着主要教程,现在我试图创建更新功能,但它给了我一个错误,我不知道为什么。更新数据库[codeIgniter]

这里是我的模型

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

控制器:

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

查看

<?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> 

------编辑-------

现在我没有错误,感谢球员,但现在我认为我的更新页面中的form_open缺少变量传递,我怎样才能让表单传递ID到我的模型?

更新视图

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

回答

2

你必须通过你输入的数据来更新新闻的模式,始终遵循MVC结构。因为代码点火器是MVC框架。

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

和模型的功能将类似于

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

仍给我同样的错误,缺少的参数和Id未定义的变量,作为一个说明,我从index.php这样调用这个'“class =”btn btn-warning“> ...' –

+0

您是否获得了$ id值? –

2

你的控制器缺少第二个参数,以便在“新闻/更新”定义$数据,$数据未在模板/头

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'); 
     $this->load->view('news/update',$data); 
     $this->load->view('templates/footer'); 

    } 
    else 
    { 

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

同样的错误,它给我'消息:缺少论点1为新闻::更新()'和'消息:未定义的变量:ID'在控制器/ News.php –

+0

检查我的代码你明确发现解决方案 –

+0

也分享你的查看以及所以我检查你如何传递你的ID –

1

您可以检查我的更新代码

这是我的模型

public function update($id) 
{ 
    $username=$this->input->post('username'); 
    $password=$this->input->post('password'); 

    $data=array(
      'user'=> $username, 
      'pass' => $password 

    ); 

    $this -> db -> where('id', $id); 
    $qq=$this->db->update('admin',$data); 
    redirect('dashboard'); 
} 

这是我的控制器

public function edit($user_id) 
{ 

    $this->db->where(['id'=>$user_id]); 
    $data['result'] = $this->db->get('admin'); 
    $this->load->view("dashboard/edit", $data); 

} 
1

也许你所要求的更新页面,如:

本地主机/控制器/更新

但更新方法需要额外的参数ID,因此对u的请求是正确的PDATE将是:

本地主机/控制器/更新/ 1

或者你也可以通过声明默认ID为更新避免错误:

public function update($id = 0) // = 0 is default id assigning 
{ 
    $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'); 
    } 
} 
1

添加合适的路由到您的“应用程序/配置/路线。PHP的”

$route['news/update/(:num)'] = 'news/update/$1'; 

确保输入的网址通过ID下面的格式

/news/update/id 

例如, /news/update/1

0

有一些失误,我想解决的问题。首先,你需要正确的模型函数

型号

public function update_news($id) 
{ 
    $this->load->helper('url'); // Best practice to load any helper is in controller 

    $slug = url_title($this->input->post('title'),'dash',TRUE); // No need to use this line in your model. If you are updating news slug then you can use it. 

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

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

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

控制器

定义的$ id = 0或不通过它在功能上,从URI字符串挑选。我宁愿不直接在功能

public function update() 
{ 
    $this->load->helper('url'); 
    $id = trim($this->uri->segment(3)) != '' && is_numeric($this->uri->segment(3)) && $this->uri->segment(3) > 0 ? $this->uri->segment($this) : false; 
    if($id == false){ 
     show_404(); 
    } 

    $this->load->helper('form'); 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('title', 'Title', 'required'); 
    $this->form_validation->set_rules('text', 'Text', 'required'); 
    if ($this->form_validation->run()) 
    { 
     $this->news_model->update_news($id); 
     $this->session->set_flashdata('success', 'Yeah! You have updated news article successfully'); 
redirect(base_url('[TO_DESIRED_URL]')); // Here [TO_DESIRED_URL] denotes to you redirect to desired url after successful update. 
    } 
    $data['title'] = 'Editar noticia'; 
    $data['id'] = $id; 
    $this->load->view('templates/header', $data); 
    $this->load->view('news/update'); 
    $this->load->view('templates/footer'); 
} 

把它作为你已经解决了显示的文章问题,所以我在这里指的更新视图的问题,在这里跳过一个视图列表。如果你还需要任何清理,那么你最欢迎。

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

<?php echo validation_errors(); ?> 

// $id passed from controller. 
<?php echo form_open('news/update/'.$id); ?> 

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

这里我已经详细解释了。如果您需要任何帮助,请告诉我。