I'm trying to assign $_POST data to a field if it differs from the $row data pulled from the SQL database. This is being used to update blogposts. My logic is if whatever is entered in the text field is different from the $row data, then assign the $_POST data to the $row array. I'm not sure where my issue is, as it does not function properly. Any help would be greatly appreciated!
<?php session_start();
include('mysqli_connect.php');
$query = "SELECT * FROM blogposts WHERE blog_id=" . $_GET['id'];
$results = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($results, MYSQLI_ASSOC);
?>
<head>
<?php include('header.html'); ?>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
background-color:#fcfcfc;
}
form {
margin:auto;
}
</style>
</head>
<body>
<form name="com" id="com" action="<?php if (($_POST['blog_content'] != NULL)
&& ($_POST['blog_title'] != NULL)) {
echo "edit_handle.php";
} else {
echo "edit_post.php?id=" . $_GET['id'];
if(($_POST['blog_title'] != NULL) && ($row['title'] !=
$_POST['blog_title'])) {$row['title'] = $_POST['blog_title'];}
if(($_POST['blog_content'] != NULL) && ($row['content'] !=
$_POST['blog_content'])) {$row['content'] = $_POST['blog_content'];}
if(($_POST['blog_title'] != NULL) && ($_POST['blog_content'] != NULL)) {
$row['title'] = $_POST['blog_title'];
$row['content'] = $_POST['blog_content'];
}
} ?>" method="post">
<?php
if (isset($_SESSION['first_name']) && ($_SESSION['user_id'] == 11)) {
echo '
Blog Title: <input type="text" value="' . $row['title'] . '"
name="blog_title" />
Post Content:<textarea name="blog_content">' . $row['content'] .
'</textarea>
<input type="submit" value="submit">'; } else {
echo '<p align="center" style="color:red">You must be logged in as
<strong>admin</strong> to post a blog!</p>';
}
?>
</form>
<?php
if (($_POST['blog_content'] != NULL) && ($_POST['blog_title'] != NULL)) {
echo "<script>document.getElementById('com').submit();</script>";
}
?>
</body>
</html>