Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:47:15+00:00 2026-06-14T12:47:15+00:00

BACKGROUND: So, I’ve been following this tutorial on how to build your own simple

  • 0

BACKGROUND: So, I’ve been following this tutorial on how to build your own simple blog/CMS using PHP and MySQL on Youtube. The CMS lets you add categories, add posts, view category list and delete and edit posts. The files consists of an index.php file, edit_post.php, add_post.php and so on. Also, there’s a blog.php file containing all functions. And there’s the config and init files. The database has two tables, posts and categories. The blog is ugly as hell, but everything works, except:

PROBLEM: When editing posts, the content of the post is moved to the post title, the cat_id is replaced with the number 0, and the content is replaced with the number 1. I can only see this info by viewing the database in phpmyadmin. As for the index.php page, the post simply disappears. I also get these two errors: ” Undefined variable: posts in C:\xampp\htdocs\blogg\resources\func\blogg.php on line 69″ and “Invalid argument supplied for foreach() in C:\xampp\htdocs\blogg\index.php on line 40”.

CODE: I know the code uses outdated mysql functions. I’ll work on that later. It’s not the problem here. Anyways, here’s the code. I really appreciate help here.

From the file containing all functions:

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}");
}

The entire edit_post.php file:

    <?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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:47:16+00:00Added an answer on June 14, 2026 at 12:47 pm

    Oh man… I’m sorry for taking your time. I discovered the problem now. After edit_post($_GET[‘id’] I used a dot instead of a comma. After changing that, everything works as it should. Small things…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background Information I'm trying to build the GAUL library according to this Instructions .
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background - I am using paramiko to put files on a bunch of remote
Background: Using unix, codeigniter from localhost. I'd like to run a controllers via a
Background I'm trying to understand matrix transformations. (Developing plugins for SketchUp using it's Ruby
Background I'm working on an application for my own needs, with some very lightweight
Background : I am using nginx as a reverse proxy to my express server
Background As CopyLocal=true causes a lot of build slowdowns as projects and solutions grow
Background info I am trying to upgrade a custom CMS to support the HTML5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.