我写了一些代码来响应我的表:

<?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?>

最后一个td元素是为特定表行删除而创建的。但是我不知道如何获取行id。这是我的delete.php文件:
<?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");
?>

我想我应该改变这一行:
<td><a href="delete.php"><span class="glyphicon glyphicon-remove"></span></a></td>

在delete.php之后应该有某种id识别器或其他东西。请帮忙。我也不知道如何创建它,因为它在一个while循环内。恐怕有些事情不太好用。

最佳答案

通过href属性将id作为参数传递

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

执行此操作时也不要忘记使用准备好的语句,因为直接将$_GET$_POST$_REQUEST参数传递到查询中是完全不安全的
使用准备好的支架
$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)

09-25 18:25
查看更多