【发布时间】:2020-07-14 19:36:22
【问题描述】:
嘿。
我正在学习 PHP 和 MYSQL atm。
所以我已经编写了代码,可以很好地将数据库中的所有内容显示为 HTML。 我还创建了一个按钮,上面写着Delete。
现在我想编写一个实际上删除特定条目(都有一个 ID)但我有点丢失的代码。 我知道命令是: $sql = "DELETE FROM cars WHERE car_id='$car_id'";但是如何在 php 中将此事件添加到按钮单击中。
我想在生成代码中我用
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "muscle_cars";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error() . "\n");
}
$sql = "SELECT car_id, carname, hp, img, available FROM cars";
$result = mysqli_query($conn, $sql);
// fetch the next row (as long as there are any) into $row
while($row = mysqli_fetch_assoc($result)) {
echo '
<div class="card col-3 m-3 bg-info" >
<img src="'.$row["img"].'" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">'.$row["carname"].'</h5>
<p class="card-text">Horsepower : '.$row["hp"].'</p>
<p class="card-text">Available : '.$row["available"].'</p>
<a href="#" class="btn btn-outline-danger text-white">DELETE</a>
</div>
</div>
';
}
// Free result set
mysqli_free_result($result);
// Close connection
mysqli_close($conn);
?>
【问题讨论】:
标签: javascript php html mysql