如何从post href

时间:2016-01-12 10:18:44

标签: php mysql mysqli

我正在尝试通过表单中的Href从DB中删除字符串。为什么这对我不起作用?查询是对的,没有任何反应。

<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("cms") or die(mysql_error());

    // 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 "<form method='POST'><a href='delete.php?id=".$row['page_id']."'>Remove</a></form>";

            echo "</li>";
        }

    echo "</table>";
    ?>

delete.php

<?php

$db = new mysqli('localhost', 'root', '', 'cms');
$id = mysqli_query($db, "SELECT page_id FROM pages");

if  (isset($_POST['id'])) {
    mysqli_query($db, "DELETE FROM pages WHERE id='".$_GET['id']."'");
}

?>

它仍然无效。只需将我发送到一个没有查询的空白页面。

1 个答案:

答案 0 :(得分:1)

改变这个:

echo "<form method='POST'><a href='delete.php?".$row['page_id']."'>Remove</a></form>";

echo "<form method='POST' action='delete.php'><input type='hidden' value=".$row['page_id']." name='id'/><input type='submit' value='Remove'/></form>";

在delete.php中

这:

if  (isset($_GET['id'])) {

if  (isset($_POST['id'])) {

mysqli_query($db, "DELETE FROM pages WHERE id='".$_GET['id']."'");

mysqli_query($db, "DELETE FROM pages WHERE id='".$_POST['id']."'");