I am working on a CMS and seem to be having issues currently with my edit code and I can't figure out what the problem is for the life of me, when I submit to edit, everything goes through as if the edit was successful, however nothing is ever changed or submitted to the database.
I have been trying many different things and nothing seems to make any difference, I am totally lost on this one.
editarticle.php
<?php
ob_start();
session_start();
include_once('includes/connection.php');
include_once('includes/news.php');
include_once('includes/functions.php');
$article = new Article;
$funct = new UserFunctions;
if (isset($_SESSION['logged_in'])) {
$articles = $article->fetch_all();
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All fields are required!';
header('Location: index.php?p=editarticle');
} else {
global $pdo;
$query = $pdo->prepare('UPDATE articles SET article_title = ?, article_content = ? WHERE article_id=?');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, $id);
$query->execute();
header('Location: index.php');
}
}
//check if an article is selected to be edited
if (isset($_GET['id'])) {
$id = $_GET['id'];
$query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
$query->bindValue(1, $id);
$query->execute();
$rows = $query->fetchAll();
//get the article title and content to put in edit inputs
foreach ($rows as $row) {
$id = $row['article_id'];
$title = $row['article_title'];
$content = $funct->br2nl($row['article_content']);
}
?>
<!-- POST -->
<div class="post">
<div class="topwrap">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="" />
<div class="status green"> </div>
</div>
<div class="icons">
<img src="images/icon1.jpg" alt="" /><img src="images/icon4.jpg" alt="" /><img src="images/icon5.jpg" alt="" /><img src="images/icon6.jpg" alt="" />
</div>
</div>
<div class="posttext pull-left">
<h2>Edit Article</h2>
<!-- add Article form start !-->
<form action="editarticle.php" method="post" autocomplete="off">
<input type="text" name="title" value="<?php echo $title; ?>" /><br /><br />
<textarea rows="10" cols="87" name="content" /><?php echo $content; ?></textarea>
<!-- add article form break !-->
</div>
<div class="clearfix"></div>
</div>
<div class="postinfobot">
<div class="dateposted pull-right">
<!-- add article form continue !-->
<input class="btn btn-primary" type="submit" value="Submit Changes" />
</form>
<!-- add article form end !-->
</div>
<div class="clearfix"></div>
</div>
</div>
<!-- POST -->
<?php
} else {
?>
<!-- POST -->
<div class="post">
<div class="topwrap">
<div class="userinfo pull-left">
</div>
<div class="posttext pull-left">
<h2>Select an Article to Edit</h2>
<?php foreach ($articles as $article) { ?>
<?php echo $article['article_id']; ?> - <a href="index.php?p=editarticle&id=<?php echo $article['article_id']; ?>"><?php echo $article['article_title']; ?></a><br />
<?php } ?>
</div>
<div class="clearfix"></div>
</div>
<div class="postinfobot">
<div class="dateposted pull-right"> </div>
<div class="clearfix"></div>
</div>
</div>
<!-- POST -->
<?php
}
} else {
header('Location: index.php');
}
?>
includes/news.php
class Article {
public function fetch_all() {
global $pdo;
$article_status = 1;
$query = $pdo->prepare("SELECT * FROM articles WHERE article_status = ? ORDER BY article_timestamp DESC");
$query->bindValue(1, $article_status);
$query->execute();
return $query->fetchAll();
}
public function fetch_data($article_id) {
global $pdo;
$article_status = 1;
$query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ? AND article_status = ?");
$query->bindValue(1, $article_id);
$query->bindValue(2, $article_status);
$query->execute();
return $query->fetch();
}
}
I am getting back into PHP for the first time in 10 years and have been doing a lot of C# development over the last 2 years. I am finding it very difficult to troubleshoot issues with PHP thus far, as I have gotten very little or no error messages to work with (not even in the error_log on my host).
Any ideas why this isn't submitting the changes to the database?