PHP code error Blog can't edit

PROBLEM:
When altering posts, the substance of the post is moved to the post title, the cat_id is supplanted with the number 0, and the substance is supplanted with the number 1. I can just see this information by review the data set in PHPMyAdmin. Concerning the index.php page, the post just vanishes. I likewise get these two blunders: " Undefined variable: posts in C:\xampp\htdocs\blogg\resources\func\blogg.PHP on line 69" and “Invalid contention provided for foreach() in C:\xampp\htdocs\blogg\index.PHP on line 40”.
CODE:
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:*
 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>

 I know the code uses outdated MySQL functions. I'll work on that later. It's not the problem here. Anyone help me
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL ^ E_NOTICE);
require_once "conn.php";

$conn=connect();
$db=connectdb();

$ic= "";
$parcelno = "";
$items = "";
if(isset($_REQUEST['ic'])){ $ic= $_REQUEST['ic']; }
if(isset($_REQUEST['parcel'])){ $parcel = $_REQUEST['parcel']; }
if(isset($_REQUEST['items'])){ $items = $_REQUEST['items']; }


mysqli_select_db($conn, $db) or die (mysqli_error($conn)."\n");
$sql="UPDATE parcel SET parcelno='".$parcel."', items='".$items."' where ic='".$ic."'";
$result=mysqli_query($conn,$sql) or die(mysqli_error($conn)."\n");

}

Use this MySQL code. I was facing the same issue on the blog page of the https://perfectessaywriting.com/ website and after the addition of this code, I got the solution.

1 Like