PHP-PDO应用程序错误


PHP - PDO app error

我正在努力扩展我的PDO知识,目前我正在php应用程序上工作。实际上是一个简单的CMS,我在管理页面上遇到了一个问题,当你试图更新现有页面时,我收到了一个关于ID[$_POST]的错误。

错误看起来像:

Notice: Undefined index: id in /Applications/MAMP/htdocs/cms/admin/edit.php on line 6 object(PDOStatement)#2 (1) { ["queryString"]=> string(138) " UPDATE pages SET label = :label, title = :title, slug = :slug, body = :body, updated = NOW(), WHERE id = :id " }  Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/cms/admin/edit.php:6) in /Applications/MAMP/htdocs/cms/admin/edit.php on line 28

EDIT.php

<?php 
require '../app/start.php';
if (!empty($_POST)) {
    $id    = $_POST['id'];
    $label = $_POST['label'];
    $title = $_POST['title'];
    $slug  = $_POST['slug'];
    $body  = $_POST['body'];
    $updatePage = $db->prepare('
        UPDATE pages
        SET label = :label, title = :title, slug = :slug, body = :body, updated = NOW(),
        WHERE id = :id
    ');
    $updatePage->execute([
        'id' => $id,
        'label' => $label,
        'title' => $title,
        'slug'  => $slug,
        'body'  => $body,
    ]);
    var_dump($updatePage);
    header('Location: ' . BASE_URL . '/admin/list.php');
    exit();
}
if(!isset($_GET['id'])){
    header('Location:' . BASE_URL . '/admin/list.php');
    exit();
}
$page = $db->prepare('
    SELECT *
    FROM pages
    WHERE id = :id
');
$page->execute(['id' => $_GET['id']]);
$page = $page->fetch(PDO::FETCH_ASSOC);

require VIEW_ROOT . '/admin/edit.php';

START.php

<?php
require ('functions.php');
ini_set('display_errors', 1);

define('APP_ROOT', __DIR__);
define('VIEW_ROOT', APP_ROOT . '/views');
define('BASE_URL', 'http://localhost/cms');
$db = new PDO('mysql:host=localhost;dbname=cms', 'root', 'root');
?>

编辑.PHP(表单页)

<?php require VIEW_ROOT . '/templates/header.php'; ?>
<h2>Add page</h2>
<form action="<?php echo BASE_URL; ?>/admin/edit.php" method="POST" autocomplete="off">
    <label for="title">
        Title
        <input type="text" name="title" id="title" value="<?php echo e($page['title']); ?>">
    </label>
    <label for="label">
        Label
        <input type="text" name="label" id="label" value="<?php echo e($page['label']); ?>">
    </label>
    <label for="slug">
        Slug
        <input type="text" name="slug" id="slug" value="<?php echo e($page['slug']); ?>">
    </label>
    <label for="body">
        Content
        <textarea name="body" id="body" cols="30" rows="10"><?php echo e($page['body']); ?></textarea>
    </label>
    <input type="hidden" value="<?php echo e($page['id']);?>">
    <input type="submit" value="Edit article">
</form>

检查以下代码:

<input type="hidden" value="<?php echo e($page['id']);?>">

正如您所看到的,此代码没有名称,根据您的示例,它应该具有name="id"