您的问题是您没有发送对要删除的数据库中记录的引用
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');
}
}
在您看来,在锚标记 url 中发送帖子的 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; ?>