簡體   English   中英

我無法在Codeigniter中刪除記錄

[英]I can't delete record in Codeigniter

我正在Codeigniter中學習CRUD。 我的表名是“ posting”,列名是這樣的(id,title,post)。 我成功創建了一個新帖子(都插入數據庫並在視圖中顯示)。 但是我刪除前端的帖子時遇到問題。 這是我的代碼:

模型

Class Post_Model extends CI_Model{
  function index(){
     //Here is my homepage code
  }

  function delete_post($id)
  {
     $this->db->where('id', $id);
     $this->db->delete('posting');
  }
}

控制者

Class Post extends CI_Controller{
  function delete()
  {
     $this->load->model('Post_Model');
     $this->Post_Model->delete_post("id");

     redirect('Post/index/', 'refresh');
  }
}

單擊主頁上的“刪除”后,沒有任何反應。 當我查看數據庫時,我的記錄仍然可用。

注意:(1)刪除記錄,我遵循codeigniter手冊/用戶指南,(2)按下前端的“刪除”按鈕后,發現消息錯誤(未定義變量:id)

有任何幫助或建議

您的問題是您沒有發送對要刪除的數據庫中記錄的引用

  Class Post extends CI_Controller{ function delete() { $this->load->model('Post_Model'); $this->Post_Model->delete_post("id"); redirect('Post/index/', 'refresh'); } } 

$this->Post_Model->delete_post("id"); 在這一行中,您需要發送對要刪除的ID的引用,並發送字符串“ id”,因此在模型中其計算結果為:

DELETE * FROM `posting` WHERE id='id';

根據您的前端,您需要修改代碼,以便它發送您要刪除的帖子的ID。 我假設您是通過POST發送信息,因此可以執行此操作,如果您通過GET發送信息,則只需將$ this-> input-> post更改為$ this-> input-> get

Class Post extends CI_Controller{
  function delete()
  {
     $id_post = $this->input->post('your_post_name') //your_post_name is the name of the field sent in the form or whatever you're using to delete the post
     $this->load->model('Post_Model');
     $this->Post_Model->delete_post($id_post);

     redirect('Post/index/', 'refresh');
  }
}

希望能幫助到你。

//編輯看到您正在嘗試從標簽中刪除,您可以執行以下操作

Class Post extends CI_Controller{
      function delete($id_post)
      {
         $this->load->model('Post_Model');
         $this->Post_Model->delete_post($id_post);

         redirect('Post/index/', 'refresh');
      }
    }

在您的視圖中,在錨標記網址中發送帖子的ID

<?php foreach ($posts->result_array() as $mypost) : ?> 
    <tr> 
        <td width="62">Title</td> 
        <td width="15" align="center">:</td> 
        <td width="380"><?php echo $mypost['title']; ?></td> 
    </tr> 
    <tr> 
        <td width="62">Deskripsi</td> 
        <td width="15" align="center">:</td> 
        <td width="380"><?php echo $mypost['post']; ?></td> 
        <!-- Here you're sending the id in the database of the post you want to delete directly to your controller, once again Im assuming
            you're obtaining the ID along with the title and the post -->
        <td width="62" align="center"><a href="<?php echo base_url(); ?>post/delete/<?php echo $mypost['id'];?>">Delete</a></td>
    </tr> 
<?php endforeach; ?>
Class Post_Model extends CI_Model{
  function index(){
     //Here is my homepage code
  }

  function delete_post($id)
  {
     $this->db->where('id', $id);
     $this->db->delete('posting');
  }
}

將post變量作為參數傳遞給delete_post()函數

Controller

Class Post extends CI_Controller{
  function delete()
  {
     $this->load->model('Post_Model');
     $this->Post_Model->delete_post($id);

     redirect('Post/index/', 'refresh');
  }
}

首先檢查您的來源,您是提交來源嗎? 如果您不使用json,則應提交輸入類型(按鈕)

表單動作應指向控制器和功能

您必須接收變量要刪除的內容

$ delete_id = $ this-> input-> post('id');

然后使用內部刪除功能

$ this-> load-> model('Post_Model');

$ this-> Post_Model-> delete_post(“ id”);

“發布”應為表名,這應該可行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM