用于从MySQL表中删除特定条目的按钮的代码(使用php)


Code for button to delete specific entries from a MySQL table (using php)?

目前,我的代码打印出一个mysql表的内容,其中包含一个删除按钮。如何使用此按钮从mysql表中删除相应的条目?此外,我如何才能使表只显示特定数据的条目(例如,不显示今天日期之前的条目)?如有任何帮助,我们将不胜感激。

<?php
include('php/connect.php');
mysql_connect ($host,$user,$password);
mysql_select_db('booking');
$sql="SELECT *FROM bookings";
$records = mysql_query($sql);
?>
<html>
<head>
<title>Records</title>
</head>
<body>
<table width = "600" border = "1" cellpadding = "1" cellspacing = "1">
<tr>
<th>ID</th>
<th>Date</th>
<th>Start</th>
<th>Name</th>
<th>E-mail</th>
<th>Phone</th>
<th>Delete</th>
</tr>
<form action="" method="post">
<?php
if(isset($_POST['delete']) and is_numeric($_POST['delete']))
{
        $sql = "";
}
while ($student = mysql_fetch_assoc ($records)){
        echo "<tr>";
        echo "<td>".$student['id']."</td>";
        echo "<td>".$student['date']."</td>";
        echo "<td>".$student['start']."</td>";
        echo "<td>".$student['name']."</td>";
        echo "<td>".$student['email']."</td>";
        echo "<td>".$student['phone']."</td>";
        echo "<td> <input type=submit name=delete id=".$student['id']." value=delete </td>";
        echo "</tr>";
}
?>
</form>
</table>
</body>
</html>

你可以试试这个。。

<a href='delete.php?id=".$student['id']."'>Delete</a>

简单地将其放在td tag中,并在delete.php中编写删除查询。

试试这个

<form action="" method="post">
<?php
if(isset($_REQUEST['delete']) and is_numeric($_REQUEST['delete']))
{
        $sql = "DELETE FROM bookings WHERE id=3";
}
while ($student = mysql_fetch_assoc ($records)){
        echo "<tr>";
        echo "<td>".$student['id']."</td>";
        echo "<td>".$student['date']."</td>";
        echo "<td>".$student['start']."</td>";
        echo "<td>".$student['name']."</td>";
        echo "<td>".$student['email']."</td>";
        echo "<td>".$student['phone']."</td>";
        echo "<td><a href='delete.php?delete=".$student['id']."'>Delete</a> </td>";
        echo "</tr>";
}
?>
</form>

*