php - 在codeigniter中发布删除id
问题描述
我有两个表users
,posts
主键在哪里id
。当我点击删除按钮时,我想发布id
.
这是我的看法:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
解决方案
有很多方法可以做到这一点,最常见的一种是从以下方式获取它url
:
$id = @end($this->uri->segment_array());
或这个:
$id = $this->uri->segment(3);
或者您可以通过将其传递到隐藏输入中来执行此操作,如下所示:
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
或者使用ajax。
推荐阅读
- javascript - 警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - Tbody
- spring - Spring 微服务架构中的 Socket.io
- reactjs - 如何使用 react-dnd 制作可拖拽到当前页面的可拖拽组件?
- wordpress - 通过 Rest API 检索特定客户的订单列表 - WooCommerce?
- angular - 当我尝试将参数发送到后端时没有反应
- angular - Gitlab CI 概述中的 Angular 11 karama-jasmine 测试结果
- c++ - QStyledItemDelegate 工具提示处理(+ 内部状态)
- php - 在 yii2 控制器中使用 composer 包
- css - Angular Bootstrap 导航栏未显示正确的样式
- docker - docker 通过 https 提供内容而没有域(仅具有公共 IP 地址)