douyao3895 2017-11-26 10:56
浏览 42
已采纳

使用php删除特定的表行

I have written some code to echo my table:

<?php while($product = mysqli_fetch_assoc($result)) : ?>
    <tr>
        <td><?php echo $product['id']; ?></td>
        <td><?php echo $product['customer_id']; ?></td>
        <td><?php echo $product['total_price']; ?></td>
        <td><?php echo $product['created']; ?></td>
        <td><?php echo $product['modified']; ?></td>
        <td><a href="delete.php"><span class="glyphicon glyphicon-remove"></span></a></td>
    </tr>
<?php endwhile?>

Last td element is created for specific table row removal. However I don't know how to get that row id. This is my delete.php file:

<?php
require_once 'core/init.php';

$id = $_GET['id'];

mysqli_query($db,"DELETE FROM orders WHERE id='".$id."'");
mysqli_close($db);
header("Location: details-modal-orders.php");
?> 

I assume i should change something in this line:

<td><a href="delete.php"><span class="glyphicon glyphicon-remove"></span></a></td>

After delete.php there should be somekind of id recognizer or something. Please help. I also don't know how to create that because it is inside of a while loop. I'm afraid something is not going to work well.

  • 写回答

2条回答 默认 最新

  • dongyu8694 2017-11-26 11:00
    关注

    Pass the id as a parameter via the href attribute

    href="delete.php?id=<?php echo $product['id']; ?>">
    

    Also do not forget to use prepared statements while performing this operation as it is totally unsafe to pass $_GET or $_POST or $_REQUEST parameters directly into your query

    Using prepared statments

    $id =  (int) $_GET['id']; //cast to integer
    
    $stmt = mysqli_stmt_prepare($db,"DELETE FROM orders WHERE id=?"); //prepare the statement returns true/fasle
    mysqli_stmt_bind_param($stmt, "i", $id); //bind placeholder to variable
    mysqli_stmt_execute($stmt); //execute (returns true/false)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法