$ _POST在提交表单和通过URL传递变量数据时不会触发

时间:2017-08-19 15:40:52

标签: php mysql pdo

简单的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语句未执行的原因将不胜感激。

2 个答案:

答案 0 :(得分:0)

$_POST用于POST方法,$_GET用于GET方法。 $_REQUEST适用于两者。

答案 1 :(得分:0)

只有按下提交按钮才会出现

$_POST['deleteitem']

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

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

您不会使用POST发送到php,而是使用GET。此外,只会传递查询字符串中存在的变量,即?listID=$listID&parentID=$parentID,因此不会传递deleteItem

因此,如果单击锚标记

,此IF测试行将永远不会成立
if(isset($_POST['deleteItem'])) {