0

I'm trying to extend my PDO knowledge and at the moment I'm working at php app. Actually is a simple CMS, and I got a problem on the admin page, when you try to update the existing pages I got an error about ID [$_POST].

The error looks like:

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');

?>

EDIT.PHP (form page)

<?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>
Alex
  • 1
  • 1

1 Answers1

0

check following code:

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

as you see this code does not have name , according to your samples it should have name="id"

Majid Abbasi
  • 1,531
  • 3
  • 12
  • 22