0

我一直在关注本教程 从 html 页面删除 mysql 行的最佳方法 - 删除链接和 php

但我被卡住了,我的行不会删除。请帮忙

// My Viewing dish Page
<?php
$result = mysql_query("SELECT * FROM dish");

echo "<table border='1'>
<tr>
<th>DishID</th>
<th>DishName</th>
<th>DishPrice</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['DishID'] . "</td>";
  echo "<td>" . $row['DishName'] . "</td>";
  echo "<td>" . $row['DishPrice'] . "</td>";
  echo '<td><a href="delete.php?id=' . $row['DishID'] . '">Delete</a></td>';
  echo "</tr>";
  }
echo "</table>";

?>


//Delete Page

<?php

require_once("database.php");


/* 
 DELETE.PHP
 Deletes a specific entry from the 'players' table
*/


 // check if the 'id' variable is set in URL, and check that it is valid
 if (isset($_GET['DishID']) && is_numeric($_GET['DishID']))
 {
 // get id value
 $DishID = $_GET['DishID'];

 // delete the entry
$result = mysql_query("DELETE FROM dish WHERE DishID =$DishID} ")
 or die(mysql_error());


 // redirect back to the view page
 header("Location: admin_modifydishes.php");
 }
 else
 // if id isn't set, or isn't valid, redirect back to view page
 {
echo "doesn'twork";
 }

?>

我相信这与 $DishID = $_GET['DishID']; 有关。但我无法绕过它,请帮助。

4

3 回答 3

4

这很简单

要删除的 URL 是:delete.php?id=your-id

要获取id参数,您需要使用$_GET['id']而不是 $_GET['DishID']

此外,您的删除查询有点错误。尝试将其替换为:

$result = mysql_query("DELETE FROM `dish` WHERE `DishID`={$DishID}");
于 2013-01-22T11:58:20.413 回答
0
$result = mysql_query("DELETE FROM dish WHERE DishID =$DishID} ")
 or die(mysql_error());

应该

$result = mysql_query("DELETE FROM dish WHERE DishID =$DishID")

$_GET['DishID']

应该

$_GET['id']
于 2013-01-22T12:00:51.813 回答
0

请注意,您在这里为自己创造了一个痛苦的世界,除非您在某处清理输入。

像这样使用 SQL 会让您很容易受到 SQL 注入攻击。如果可能的话,你应该看看 PDO 模块。

您还应该看看http://www.phptherightway.com/#databases

于 2013-01-22T12:01:35.903 回答