【问题标题】:PHP and HTML Deleting [closed]PHP和HTML删除[关闭]
【发布时间】:2011-10-05 16:24:48
【问题描述】:

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

<?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>

我想删除包含行的超链接。

【问题讨论】:

  • 需要更多信息(即您希望删除发生在服务器端还是客户端?)。此外,无论第一个问题的答案如何,对 stackoverflow 的搜索都可能会产生该问题的现有解决方案。

标签: php html mysql delete-row


【解决方案1】:

你需要在删除url中添加你要删除的记录的ID,例如:

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 注入漏洞毁了你的一天。

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

【讨论】:

  • 谢谢它的 ID 位,我遇到了麻烦,下次我可以实现它!
【解决方案2】:

您需要将行的 ID 作为参数传递给 delete.php 脚本

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

【讨论】:

    【解决方案3】:

    您可以使用当前行的id并将其发送到delete.php:

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

    然后在 delete.php 中通过$deleteId = $_GET['id'] 获取 id 并使用它...

    【讨论】:

      【解决方案4】:

      尝试将 URL 更改为 echo '&lt;a href="delete.php?ID=' . $row['ID'] . '"&gt;' 之类的内容

      顺便说一句 - 使用 root 不是一个好主意!

      【讨论】:

      • 感谢您的建议,我会尽快更改此设置!谢谢你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2013-03-28
      相关资源
      最近更新 更多