2017-04-24 96 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> 

這裏我已經詳細解釋了。如果您需要任何幫助,請告訴我。