gpt4 book ai didi

PHP 和 HTML 删除

转载 作者:行者123 更新时间:2023-11-29 14:42:44 24 4
gpt4 key购买 nike

我只是想要表格中的一个按钮来删除特定行,但我搜索的所有内容最终都与我的设置方式完全不同。

<?php

// Connects to your Database and if it cant it dies
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// this selects the db
mysql_select_db("test", $con);

// this passes the query into the variable result
$result = mysql_query("SELECT * FROM items");

echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Quantity</th>
<th>Delete</th>
<tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . '<a href="delete.php">Delete</a>' . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
//end php
?>

<html>
<body>

<form action="insert.php" method="post">
Name: <input type="text" name="Name" />
Quantity: <input type="text" name="Quantity" />
<input type='submit' name='add' value='add' />
</form>

</body>
</html>

我想要删除包含在其中的行的超链接。

最佳答案

您需要将要删除的记录的ID添加到删除url中,例如:

echo "<td><a href=\"delete.php\?id={$row['ID']}">Delete</a></td>";

然后,在您的delete.php脚本中,您可以执行以下操作:

<?php

$id = intval($_GET['id']);
$sql = "DELETE FROM yourtable WHERE id=$id";
$result = mysql_query(sql);

当然,这不是完整的脚本,但向您展示了需要完成的基本操作。在查询中使用传入的值时要小心 - 不想让 SQL 注入(inject)漏洞毁了您的一天。

但是,请注意,对此类事情使用“GET”类型查询通常是一个坏主意。如果此页面被 Google 抓取(仅此而已),您会发现简单的抓取行为就毁掉了您的所有记录。

关于PHP 和 HTML 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7664531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com