为什么这不起作用?我想被重定向到id?1并且remove.php将其从表中删除。这是怎么做到的?
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages")
or die(mysql_error());
echo "<table border='1'>";
while ($row = mysql_fetch_array($result)) {
echo "<li class='list-group-item'>";
echo $row['header'];
echo "<br/>";
echo $row['description'];
echo "<br/>";
echo "<button type='button' class='btn btn-default btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ändra innehåll</button>";
echo " ";
echo "<form method='POST'><a href='delete.php?". $row['page_id']."' name='action' class='btn btn-danger btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ta bort sida</a></form>";
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
echo "</li>";
}
echo "</table>";
?>
delete.php
<?php
if (isset($_POST['1']))
{
foreach ($_POST['id'] as $page_id)
{
if (!mysql_query("DELETE FROM pages WHERE id = '$page_id'"))
{
echo mysql_error();
}
}
}
?>
请帮帮我。提前谢谢。
答案 0 :(得分:2)
你不能在POST中获取id,因为你将它用作查询字符串。你可以得到它:
$page_id = intval( $_GET['id'] );
现在您可以将其检查为:
if( $page_id > 0 ){
// your stuff.. use this id in your query
}
我改变了什么:
使用$ _GET(SUPER GLOBAL)代替$ _POST,因为查询字符串。
旁注:
没有必要使用foreach循环,因为它不是数组。
我建议您使用mysqli_ *或PDO而不是mysql_ *因为它已被弃用且在PHP 7中不可用。