我正试图创造一个小问题。 我写了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 !";
}
?>
fetchint data 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>
delete.php
<?php
include_once 'connect.php';
mysql_query("DELETE FROM add_data WHERE **id=2"**);
header('location:select.php');
?>
现在在删除文件中我不想写id = 2因为它只会删除第二条记录我要删除我点击的内容。
答案 0 :(得分:0)
为delete.php脚本提供id并使用$ _GET检索它,然后使用检索到的id
使用where子句执行删除echo $row["name"]."<a href=\"delete.php?id=".$row["id"]."\">delete</a>"."<br/>";
在delete.php中
$ id = isset($ _ GET ['id'])? (int)$ _ GET ['id']):0;
答案 1 :(得分:0)
在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');
}