[英]How do i delete the records from the table?
I am using a form to get the input of username and password to store the value into the database, once the form is submitted i have defined a table to show all the values from the users table, it have 3 fields (id, name, pass) i want to delete each record by it's id . 我正在使用表单获取用户名和密码的输入,以将值存储到数据库中,提交表单后,我定义了一个表格来显示用户表中的所有值,它具有3个字段(ID,名称,我想通过其ID删除每条记录。
i am fetching the data from the users table by using the following code: 我正在使用以下代码从用户表中获取数据:
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "</tr>"; }
i want to add the delete hyperlink to delete the particular records by id. 我想添加删除超链接以按ID删除特定记录。
i tried using the following code and i couldnt achieve it. 我尝试使用以下代码,但我无法实现。
if(mysql_num_rows($result_select) > 0) {
if(isset($_POST['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_POST['id'];
$result_delete = mysql_query($query_delete);
}
echo "<table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td><a href=".$_SERVER['PHP_SELF']."?id=".$row[id].">Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
I am a newbie to programming, i would appreciate if someone explain me in simple words.. thank you :) 我是编程的新手,如果有人用简单的话解释我,我将不胜感激..谢谢:)
First of all put your delete code in your page like this: 首先,将您的删除代码放入页面中,如下所示:
if(isset($_POST['submit'])) {
for($i = 0; $i < count($_POST['del']); $i++)
{
// check which records to delete
if (isset($_POST['del'][$i]))
{
$query_delete = "DELETE FROM users WHERE id = " . (int) $_POST['del'][$i];
$result_delete = mysql_query($query_delete) or die(mysql_error());
}
}
echo 'Record Deleted !!' . '<br /><br />';
}
Later put your select code and modify it like this: 稍后放入您的选择代码并按如下所示进行修改:
echo '<form action="" method="POST">';
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td><input type=\"checkbox\" name=\"del[]\"></td>";
echo "</tr>";
}
echo '<input type="submit" name="submit">';
echo '</form>';
You need to change $_POST
to $_GET
, so this should work. 您需要将
$_POST
更改$_POST
$_GET
,这样就可以了。
if(isset($_GET['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_GET['id'];
$result_delete = mysql_query($query_delete);
and also put values of attributes inside double quotes, like this 并将属性值放在双引号内,像这样
echo '<td><a href="'.$_SERVER['PHP_SELF'].'?id='.$row[id].'">Delete</a></td>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.