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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度