【问题标题】:Simple PHP blog: Can't edit post简单的 PHP 博客:无法编辑帖子
【发布时间】:2012-11-06 17:39:40
【问题描述】:

背景:所以,我一直在关注本教程,了解如何在 Youtube 上使用 PHP 和 MySQL 构建您自己的简单博客/CMS。 CMS 允许您添加类别、添加帖子、查看类别列表以及删除和编辑帖子。这些文件由 index.php 文件、edit_post.php、add_post.php 等组成。此外,还有一个包含所有函数的 blog.php 文件。还有配置和初始化文件。该数据库有两个表,帖子和类别。博客丑得要命,但一切正常,除了:

问题: 编辑帖子时,帖子内容移动到帖子标题,cat_id被数字0替换,内容被数字1替换。我只能通过在 phpmyadmin 中查看数据库来查看此信息。至于 index.php 页面,帖子就消失了。我还收到这两个错误:“未定义变量:第 69 行 C:\xampp\htdocs\blogg\resources\func\blogg.php 中的帖子”和“C:\xampp\htdocs\ 中为 foreach() 提供的参数无效blogg\index.php 第 40 行"。

代码: 我知道代码使用了过时的 mysql 函数。我稍后会解决这个问题。这不是这里的问题。无论如何,这是代码。非常感谢这里的帮助。

来自包含所有函数的文件:

function edit_post($id, $title, $contents, $category) {
    $id         = (int) $id;
    $title      = mysql_real_escape_string($title);
    $contents       = mysql_real_escape_string($contents);
    $category       = (int) $category;

    mysql_query(" UPDATE `posts` SET
                    `cat_id`    = {$category},
                    `title`     = '{$title}',
                    `contents`  = '{$contents}'
                 WHERE `id` = {$id}");
}

整个edit_post.php文件:

    <?php
include_once('resources/init.php');

$post = get_posts($_GET['id']);

if ( isset($_POST['title'], $_POST['contents'], $_POST['category']) ) {
    $errors = array();

    $title      = trim($_POST['title']);
    $contents   = trim($_POST['contents']);

    if ( empty ($title) ) {
        $errors[] = 'Skriv in titel';
    }

    if ( empty ($contents) ) {
        $errors[] = 'Skriv in text';
    }

    if ( ! category_exists('id', $_POST['category']) ) {
        $errors[] = 'Kategorin finns inte';
    }

    if ( strlen($title) > 255) {
        $errors[] = 'Titeln får inte vara längre än 255 tecken';
    }

    if ( empty($errors) ) {
        edit_post($_GET['id']. $title, $contents, $_POST['category']);

        header("Location: index.php?id={$post[0]['post_id']}");
        die();
    }
}

?>


<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <style>
    label { display: block; }
    </style>

    <title> Edit a Post </title>
</head>

<body>
    <h1> Edit a Post </h1>

    <?php

    if ( isset($errors) && ! empty($errors) ) {
        echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
    }
    ?>

    <form action="" method="post">
        <div>
            <label for="title"> Title </label>
            <input type="text" name="title" value="<?php echo $post[0]['title']; ?>">
        </div>
        <div>
            <label for="contents"> Contents </label>
            <textarea name="contents" rows="15" cols="50"><?php echo $post[0]['contents']; ?></textarea>
        </div>
        <div>
            <label for="category"> Category </label>
            <select name="category">
                <?php
                foreach ( get_categories() as $category ) {
                    $selected = ($category['name'] == $post[0]['name']) ? ' selected' : '';
                    ?>
                    <option value="<?php echo $category['id']; ?>" <?php echo $selected; ?>> <?php echo $category['name']; ?> </option>
                    <?php
                }
                ?>
            </select>
        </div>
        <div>
            <input type="submit" value="Edit Post">
        </div>
    </form>
</body>
</html>

【问题讨论】:

  • @GeekNum88,“我知道代码使用了过时的 mysql 函数。我稍后会处理。这不是问题所在。”
  • 您能否突出显示代码中的错误行?请问get_categories()get_posts() 的代码也可以用吗?
  • @Jeffrey 错过了该声明
  • 对不起,我错过了一个逗号,而是使用了一个点。很烦人。无论如何,谢谢!

标签: php mysql database content-management-system


【解决方案1】:

哦,伙计……对不起,耽误了您的时间。我现在发现了问题。在 edit_post($_GET['id'] 之后,我使用了一个点而不是逗号。更改之后,一切正常。小事......

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多