PHP - PDO 应用程序错误

标签 php mysql pdo

我正在尝试扩展我的 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

编辑.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');

?>

EDIT.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 ,根据您的示例,它应该具有 name="id"

关于PHP - PDO 应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240831/

相关文章:

javascript - 来自 ajax 调用的 symfony 中的表单验证

PHP 避免重复相同的 try catch

php - 遍历 PHP 数组以显示图表

mysql - 插入时获取 "Mysql::Error: Lock wait timeout exceeded"

mysql - 从子查询 SQL 返回所有行?

php - 想要在图像下载过程中显示 “loading” 图像 - Javascript

php - 与 ereg_replace() 的混淆 开始 PHP 和 MySQL 示例,作者:W Jason Gilmore

php - PDO::PARAM 用于十进制类型?

php - PDO 和嵌套抓取

PHP登录功能找不到正确的用户名和密码