Reputation:
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>
Upvotes: 0
Views: 1419
Reputation:
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...
Upvotes: 1