简单的CRUD应用程序。
主清单上有几个孩子
删除孩子时,我会将parentID和childID传递给我的deleteItem脚本“ deleteitem.php”。

从数据库信息呈现表

<form action="deleteitem.php" method="post">

<?php

$var = 1;
$listID = $_GET['listID'];

$select = "SELECT subtasks.createdOn, subtasks.subtask_id, subtasks.listTitle, subtasks.createdBy, subtasks.listStatus, subtasks.createdOn
           FROM subtasks
           INNER JOIN userlists
           ON subtasks.parentID = userlists.listID
           WHERE subtasks.isChild = :var
           AND subtasks.parentID = :listID";
$select = $pdo->prepare($select);
$select->bindParam(':var',$var);
$select->bindParam(':listID',$listID);
$select->execute();

while ($row = $select->fetch(PDO::FETCH_ASSOC)) {

    $listID = $row['subtask_id'];
    $listTitle = $row['listTitle'];

    echo "<tr>";
    echo "<td>".$createdOn = $row['createdOn']."</td>";
    echo "<td>".$listTitle = $row['listTitle']. "</td>";
    echo "<td>".$listStatus = $row['listStatus']."</td>";
    echo "<td><a class=\"btn btn-warning\" href='edit.php?edit_id=$listID'";
    echo "</a><i class=\"fa fa-pencil-square-o\"></i>";
    echo "<a>";
    echo " ";
    echo "<a href='deleteitem.php?listID=$listID&parentID=$parentID'>";
    echo "<input=\"submit\" class=\"btn btn-danger\" value=name=\"deleteItem\"/>";
    echo "<i class=\"fa fa-trash-o\"></i></a>";
    echo "<a></td>";
    echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
?>

</tbody>
</form>


deleteitem.php

<?php

session_start();

if(!isset($_SESSION['user_id'])) {
    header("Location: index.php");
}

/**Include our MySQL connection.*/
require 'database.php';

$listID = $_GET['listID'];
$parentID = $_GET['parentID'];

try {
    if(isset($_POST['deleteItem'])) {

        $delete = "DELETE FROM subtasks
                   WHERE subtask_id = :listID
                   AND parentID = :parentID";
        $delete = $pdo->prepare($delete);
        $delete->bindParam(':listID',$listID);
        $delete->bindParam(':parentID',$parentID);
        $delete->execute();

        $listID = $parentID;

        header("location:list.php?listID=$listID&parentID=$parentID");
        echo 'The delete happened';
    }
    /* catch any exceptions that may occur */
} catch(PDOException $e) {
    echo "error:".$e->getMessage();
}

echo 'Parent ID ';
var_dump($parentID);
echo '<br/>';
echo 'List ID ';
var_dump($listID);

?>


我的var_dump行正在输出parentID和listID,因此我知道数据已被推送到deleteitem.php文件。但是由于某种原因,DELETE语句根本没有运行。

弄清楚为什么DELETE语句不执行的任何帮助将不胜感激。

最佳答案

$_POST['deleteitem']仅在您按下提交按钮时出现。

如果单击在此处创建的锚标记:

echo "<a href='deleteitem.php?listID=$listID&parentID=$parentID'>";


您将不会使用POST发送到php,而是使用GET发送。同样,仅传递存在于查询字符串(即?listID=$listID&parentID=$parentID)上的变量,而不传递deleteItem

因此,如果您单击锚标记,则此IF测试行将永远不会为真

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

10-02 16:01