我正在尝试创建一个crud。 我写了 php 查询来删除获取的数据。但我不知道在删除查询中 ID= ""之后要写什么。我的代码如下
索引文件
<html>
<head>
<title>crud system</title>
</head>
<body>
<?php include 'connect.php';?>
<form method="post" action="postdata.php">
username:
<input type="text" name="name">
<input type="submit" name="submit">
</form>
</body>
</html>
postdata.php
//postdata.php
<?php
mysql_connect('localhost','root','') or die("cannot to database");
mysql_select_db('users');
$name = $_POST['name'];
$sql="INSERT into add_data (name) values('$name')";
$query=mysql_query($sql);
if(!$query){
echo"data entrance failed".mysql_error();
}
else{
echo "data added successfully !";
}
?>
获取数据select.php
<!DOCTYPE html>
<html>
<body>
<?php
include 'connect.php';
$result = mysql_query("SELECT * FROM add_data");
while($row = mysql_fetch_array($result)) {
echo $row["name"]."<a href=\"delete.php\">delete</a>"."<br/>";
}
?>
</body>
</html>
删除.php
<?php
include_once 'connect.php';
mysql_query("DELETE FROM add_data WHERE **id=2"**);
header('location:select.php');
?>
现在在删除文件中我不想写 id=2 因为它只会删除第二条记录我想删除我点击的任何内容。
最佳答案
在 select.php 中
while($row = mysql_fetch_array($result))
{
echo $row["name"].'<a href="delete.php?del_id='.$row["id"].'">Delete</a><br/>';
}
在 delete.php 中
$del_id = '';
if(!empty($_GET['del_id']))
{
$del_id = $_GET['del_id'];
include_once 'connect.php';
mysql_query("DELETE FROM add_data WHERE id = '$del_id' ");
header('location:select.php');
}
关于php - 我想使用 php 从数据库中删除我单击的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37414481/