0

I'm new to PHP. I'm trying to make a "create post" form in admin, everything works fine before sending the form value. But after submiting the form, the checkbox won't display anymore, and returns me "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /opt/lampp/htdocs/testing/admin/includes/new_post.php on line 23" and "Commands out of sync; you can't run this command now" with the mysqli_error() function.
Here's the code for the form

<?php create_post(); ?>

<form action="" method="post" enctype="multipart/form-data">


    <div class="form-group">
        <label for="title">Post Title</label>
        <input type="text" class="form-control" name="title">
    </div>
    <div class="form-group">
        <label for="author">Author</label>
        <input type="text" class="form-control" name="author">
    </div>
    <div class="form-group">
        <label for="categories">Categories</label><br>
        <?php

            $query = "SELECT * FROM categories";
            $get_cat = mysqli_query($connection, $query);
            check_error($get_cat);
            while($row = mysqli_fetch_assoc($get_cat)) {
                $cat_id = $row["cat_id"];
                $cat_name = $row["cat_name"];
                echo "<input type='checkbox' name='category[]' value='$cat_id'>$cat_id, $cat_name";
            }


        ?>
    </div>
    <p class="bg-danger"><?php echo $outputMsg; ?></p>
    <div class="form-group">
        <label for="tags">Tags</label>
        <input type="text" class="form-control" name="tags">
    </div>
    <div class="form-group">
        <label for="post_status">Status</label>
        <input type="text" class="form-control" name="status">
    </div>
    <div class="form-group">
        <label for="image">Image</label>
        <input type="file" name="image">
    </div>
    <div class="form-group">
        <label for="content">Post Content</label>
        <textarea class="form-control" id="" cols="30" rows="10" name="content"></textarea>
    </div>
    <div class="form-group">
        <input type="submit" class="btn btn-primary" value="Publish" name="submit">
    </div>



</form>

And this is the function

<?php

    $outputMsg = "";

function check_error($sqli_query) {
    global $connection, $outputMsg;
    if(!$sqli_query) {
        $outputMsg = "ERROR OCCUR!" . mysqli_error($connection);
    }

}



function create_post() {
        global $connection, $outputMsg;

        if(isset($_POST["submit"])) {
            $title = $_POST["title"];
            $author = $_POST["author"];
            $status = $_POST["status"];
            $tags = $_POST["tags"];
            $content = $_POST["content"];
            $date = date('d-m-y');
            $comment_count = 20;
            $image = $_FILES['image']['name'];
            $image_temp = $_FILES['image']['tmp_name'];
            $cats = $_POST["category"];



            if(strlen($title) <= 2) {
                echo "Title must be 3 characters or more";
            } elseif(strlen($author) <= 2) {
                echo "Author must be 3 characters or more";
            } elseif(!$status) {
                echo "Pls input status for this post";
            } elseif(!isset($_POST["category"])) {
                echo "Pls choose which category this post belongs to";
            } elseif(strlen($tags) <= 4) {
                echo "Tags must be 5 characters or more";
            } elseif(strlen($content) <= 30) {
                echo "Content must be 31 characters or more";
            } else {

                move_uploaded_file($image_temp, "../images/$image");
                $query  = "INSERT INTO posts (post_title, post_date, post_author, post_tags, post_status, post_image, post_content, post_comment_count)";
                $query .= "VALUES ('$title', now(), '$author', '$tags', '$status', '$image', '$content', $comment_count);";

                $create_post = mysqli_query($connection, $query);
                check_error($create_post);

                if($create_post) {
                    $new_post_id = mysqli_insert_id($connection);
                    $new_query = "";

                    foreach($cats as $cat) {
                        $new_query .= "INSERT INTO post_cat (Post_ID, Category_ID) VALUES ('$new_post_id', '$cat');";
                    }

                    $post_cat_relationship = mysqli_multi_query($connection, $new_query);
                    check_error($post_cat_relationship);
                }

            }

        }



}

?>

The data was successfully sent to the database after submitting, but after that, the checkbox in the admin page won't display and it's getting error.
The problem stops, after i comment the if($create_post) {...} part out

vid Proli
  • 27
  • 4

0 Answers0