通过 PHP / HTML 按钮删除 MYSQL 数据库条目

嘿。


我正在学习 PHP 和 MYSQL atm。


所以我已经编写了代码,可以将数据库中的所有内容很好地显示为 HTML。我还创建了一个按钮,上面写着“删除”。


现在我想编写一个代码,它实际上删除特定的条目(都有一个ID),但我有点迷失了。我知道命令是: $sql = "DELETE FROM cars WHERE car_id='$car_id'"; 但是我如何在 php 中将此事件添加到按钮单击中。


我认为在生成代码中我将汽车 id 添加到按钮 <button id="'.$row["car_id".' ”然后当我单击按钮时以某种方式神奇地删除该条目,但我卡住并迷失了如何调用它。


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

?>


猛跑小猪
浏览 160回答 3
3回答

心有法竹

你有两种方法:1:使用GET方法:<a href="/delete.php?id='.$row['car_id'].'" class='btn btn-outline-danger text-white'>DELETE</a>在delete.php中:$id=$_GET['id'];并查询删除。2:使用ajax:&nbsp;<button value="'.$row['car_id'].'" class="btn btn-outline-danger text-white" onclick="Delete(this)">DELETE</button>js:&nbsp; &nbsp;function Delete(elem) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var id= elem.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "delete.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {id: id},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Done');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }在delete.php中:&nbsp;$id=$_POST['id'];并查询删除。请改进您的所有查询。(准备 stmt)

慕容3067478

您可以使用带有操作按钮名称的表单来标识要执行操作的哪一部分&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST['delete_rows'])) { //<-- see input name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ... delete anything from database ...&nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST['select_rows'])) { //<-- see input name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ... select anything from database ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; ?>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <form method="post">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="delete_rows" value="Delete"/>&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="select_rows" value="Select"/>&nbsp; &nbsp; </form>

梦里花落0921

您可以通过两种方式完成此操作1-您可以创建一个超链接并将行值传递给它,就像<a herf="page.php ? Id= $row['id']>del</>2-您可以在表单方法 post 中创建一个按钮并发布 id 并删除记录。注意:在 php 中,它使用名称标签而不是 id我希望这个答案对你有帮助
打开App,查看更多内容
随时随地看视频慕课网APP