简体   繁体   English

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

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

Currently, my code prints out the contents of a mysql table and it contains a delete button. 目前,我的代码打印出mysql表的内容,并且包含一个删除按钮。 How can I get this button to delete the corresponding entry from the mysql table? 如何获得此按钮以从mysql表中删除相应的条目? Also, how can I make it so that the table only displays entries of a certain data (eg do not display entries from before the today's date)? 另外,如何使表格仅显示某些数据的条目(例如,不显示今天日期之前的条目)? Any help would be greatly appreciated. 任何帮助将不胜感激。

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

You can try this.. 你可以试试看

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

Simple put this in td tag and write query for delete in delete.php . delete.php将其放在td tag然后在delete.php编写删除查询delete.php

Try this 尝试这个

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

* *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM