我只想添加一个删除按钮,可以删除数据库中的1个特定行。
我的脚本输出如下:
用户名12334
username1 12334
But i want something like this-:
username 12334 delete
username1 12334 delete
这样如果我点击第二行的删除,那么我希望它从数据库中删除。
$con=mysqli_connect("localhost","root","","****");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$list="select d_name,d_amount from donated order by d_id desc limit 10;";
$data=mysqli_query($con,$list);
echo "<table border='1'>";
echo "<tr><td>" ."<strong>NAME OF DONER(s)</strong>" . "</td><td>" . "<strong>AMOUNT DONATED</strong>" . "</td></tr>";
while($info = mysqli_fetch_array($data))
{
echo "<tr><td>" . $info['d_name'] . "</td><td>" . $info['d_amount'] . "</td></tr>";
}
echo "</table>";
答案 0 :(得分:0)
这应该有效:
$con=mysqli_connect("localhost","root","","****");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['delete'])){ // if there is id in current url
$id = intval($_GET['delete']); // parse it
mysqli_query($con, 'DELETE FROM donated WHERE d_id = ' . $id); // and remove row from database
}
$list="select d_name,d_amount,d_id from donated order by d_id desc limit 10;"; //added id to select statement
$data=mysqli_query($con,$list);
echo "<table border='1'>";
echo "<tr><td>" ."<strong>NAME OF DONER(s)</strong>" . "</td><td>" . "<strong>AMOUNT DONATED</strong>" . "</td></tr>";
while($info = mysqli_fetch_array($data))
{
echo "<tr><td>" . $info['d_name'] . "</td><td>" . $info['d_amount'] . "</td><td><a href='?delete=" . $info['d_id'] . "'>delete</a></td></tr>"; // we add delete url here
}
echo "</table>";
答案 1 :(得分:0)
像这样设置您的查询,
<?php
$list="select d_id,d_name,d_amount from donated order by d_id desc limit 10;";
$data=mysqli_query($con,$list);
echo "<table border='1'>";
echo "<tr><td>" ."<strong>NAME OF DONER(s)</strong>" . "</td><td>" . "<strong>AMOUNT DONATED</strong>" . "</td>
<td> DELETE </td></tr>";
while($info = mysqli_fetch_array($data))
{
echo "<tr><td>" . $info['d_name'] . "</td><td>" . $info['d_amount'] . "</td>";
?>
在此处传递您的ID
<td> <a href="<?php echo "your_page.php?action=delete&id=".$info['d_id']; ?>"> delete </a> </td> </tr>
<?php
}
echo "</table>";
?>
现在抓住您页面顶部的ID&amp;运行删除查询,
<?php
if(isset($_GET['action']) && $_GET['action'] == 'delete')
{
$id = $_GET['id'];
$query = "delete from donated where d_id = '$id'";
}
?>