I am making a edit blog option for a website. I am done with the code. Whenever i click the edit button it redirects to the form action file and shows a blank page. Here are my codes.

edit-blog.php
<?php
include_once('header.php');
include_once('sidebar.php');
include_once('controller/BlogController.php');

$_SESSION['ap-old']['title'] = $_POST['title'];
$_SESSION['ap-old']['short_description'] = $_POST['short_description'];
$_SESSION['ap-old']['description'] = $_POST['description'];

$title = (!empty($_SESSION['ap-old']['title'])) ? $_SESSION['ap-old']['title'] : '';
$short_description = (!empty($_SESSION['ap-old']['short_description'])) ? $_SESSION['ap-old']['short_description'] : '';
$description = (!empty($_SESSION['ap-old']['description'])) ? $_SESSION['ap-old']['description'] : '';

?>
<style type="text/css">
    .ff_fileupload_start_upload, #additional-image-html {
        display: none !important;
    }
    label {
        font-size: 13px;
        color : #6a994e;
    }
</style>
<!--app-content open-->
<div class="app-content main-content mt-0">
    <div class="side-app">
        <!-- CONTAINER -->
        <div class="main-container container-fluid">
            <!-- PAGE-HEADER -->
            <div class="page-header">
                <div>
                    <h1 class="page-title">Edit Blog </h1>
                </div>
                <div class="ms-auto pageheader-btn">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Home</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Edit Blog</li>
                    </ol>
                </div>
            </div>
            <!-- PAGE-HEADER END -->
            <?php
               if(isset($_GET['id']))
               {

                   $post_id = $_GET['id'];
                   $post_query = "SELECT * FROM blogs WHERE id = '$post_id' LIMIT 1";
                   $post_query_res = mysqli_query($con ,$post_query);

                   if(mysqli_num_rows($post_query_res)>0){
                    $post_row = mysqli_fetch_array($post_query_res);
                    ?>

                        <form action="requestHandler.php" method="POST" enctype="multipart/form-data" id="edit-blog-form">
                        <input type="hidden" name="action" value="edit-blog">
                        <input type="hidden" name="blog_id" value="<?=$post_row['id']?>">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="card">
                                        <div class="card-header border-bottom">
                                            <h3 class="card-title">Blog Details</h3>
                                        </div>


                                        <div class="card-body row">
                                            <div class="col-12" style="margin: auto;">
                                                <div class="form-group">
                                                    <label for="thumbnail-img">Blog Name : </label>
                                                    <input name="title" type="text" class="form-control" id="title" value="<?= $post_row['title']?>">
                                                </div>
                                                <div class="form-group">
                                                   <label for="thumbnail-img">Thumbnail Image : </label>
                                                   <input required name="thumbnail" id="thumbnail" type="file" class="dropify" data-height="200" />
                                                </div>
                                                <div class="form-group">
                                                    <label for="thumbnail-img">Blog Short Content : </label>
                                                    <textarea name="short_description" type="text" class="form-control" id="short_description"><?= $post_row['short_description']?></textarea>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="card">
                                        <div class="card-header border-bottom">
                                            <h3 class="card-title">Blog Content</h3>
                                        </div>
                                        <div class="card-body">
                                            <textarea id="description" class="content" name="description"><?= $post_row['description']?></textarea>
                                        </div>
                                        <div class="form-group text-center">
                                            <button type="submit" name="app-blog-btn" id="app-blog-btn" class="btn btn-success btn-md">Edit Blog</button>
                                        </div>
                                    </div>
                                </div>
                            </div>


                        </div>

                    <?php

                   }

               }
            ?>




        </div>
    </div>
    <!-- CONTAINER END -->
</div>

<!-- FOOTER -->
<?php include_once('footer.php'); ?>
<!-- FOOTER END -->

</div>

<?php include_once('scripts.php'); ?>
<script src="assets/plugins/wysiwyag/jquery.richtext.js"></script>
<script src="assets/plugins/wysiwyag/wysiwyag.js"></script>
<script src="assets/plugins/quill/quill.min.js"></script>
<script src="assets/js/form-editor2.js"></script>
<script type="text/javascript">
    $(document).ready(function(){

        $("#app-blog-btn").click(function(){
            const fields = [
                ['title', ['required','min-length:5','max-length:255']],
                ['description', ['required','min-length:10']]
            ];

            const status = validateData(fields);

            if(status !== true){
                $(".alert-msg").html(status);
                $(".alert-danger").css("display","block");
                $('#app-blog-btn').prop('disabled', false);
            } else {
                $("#edit-blog-form").submit();
            };
        });
    });
</script>
</body>

</html>
requestHandler.php
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
if(!empty($_GET['action'])){
    if(in_array($_GET['action'], ['get-cart-items'])){
        include_once("controller/CartController.php");
        $action = $_GET['action'];
        if($action == 'get-cart-items'){
            echo getCartItems();
        }
    }
}

if(empty($_POST['action'])){
    return null;
}

if(in_array($_POST['action'], ['add-blog', 'toggle-blog', 'add-product', 'edit-product','edit-blog', 'update-order-status', 'update-delivery-status'])){
    $action = $_POST['action'];
    if($action == 'add-product'){
        include_once("controller/ProductController.php");
        return addProduct($_POST, $_FILES);
    } else if($action == 'add-blog'){
        include_once("controller/BlogController.php");
        return addBlog($_POST, $_FILES);
    } else if($action == 'toggle-blog'){
        include_once("controller/BlogController.php");
        echo toggleBlog($_POST);exit;
    } else if($action == 'edit-blog'){
        include_once("controller/BlogController.php");
        return editBlog($_POST, $_FILES);
    } else if($action == 'edit-product'){
        include_once("controller/ProductController.php");
        return editProduct($_POST, $_FILES);
    } else if($action == 'update-order-status'){
        include_once("controller/AdminController.php");
        echo updateOrderStatus($_POST);
    } else if($action == 'update-delivery-status'){
        include_once("controller/AdminController.php");
        echo updateDeliveryStatus($_POST);
    }
}



if(in_array($_POST['action'], ['add-to-cart', 'remove-item-from-cart'])){
    include_once("controller/CartController.php");
    $action = $_POST['action'];
    if($action == 'add-to-cart'){
        echo addToCart($_POST);
    } else if($action == 'remove-item-from-cart'){
        echo removeItemFromCart($_POST);
    }
}

if(in_array($_POST['action'], ['place-order'])){
    include_once("controller/CheckoutController.php");
    $action = $_POST['action'];
    if($action == 'place-order'){
        echo placeOrder($_POST);
    }
}

//Auth Modules
if(in_array($_POST['action'], ['add-user','get_ref_name','signin','signup','forgot'])){
    include_once("controller/AuthController.php");
    $action = $_POST['action'];
    if($action == 'add-user'){
        echo addUser($_POST); exit;
    } else if($action == 'get_ref_name'){
        echo getRefName($_POST);
    } else if($action == 'signin'){
        echo login($_POST); exit;
    } else if($action == 'signup'){
        echo signup($_POST); exit;
    } else if($action == 'forgot'){
        return forgot($_POST);
    }
}



?>
BlogController.php
<?php
include_once("conf.php");
include_once("modals/blogModal.php");
$bm = new blogModal($db);

function getAllBlogs(){
    global $bm;
    return $bm->getAllBlogs();
}
function toggleBlog($data){
    global $bm;
    return $bm->toggleBlog($data['blog_id']);
}

function getBlogId($id){
    global $pm;
    return $pm->getBlogId($id);
}

function addBlog($data, $files){
    global $bm;
    if(!is_admin()) exit;

    $_SESSION['ap-old']['name'] = $data['blog-name'];
    $_SESSION['ap-old']['editor'] = $data['editor-style01'];

    if(empty($files) || empty($files['thumbnail-img']) || empty($files['thumbnail-img']['size']) || $files['thumbnail-img']['size'] < 1){
        appRedirect("error","add-product","Invalid Thumbnail File");exit;
    }

    $image_name = uploadImage('blog-images', $files, 'thumbnail-img');

    if($image_name == 0){
        appRedirect("error","add-blog","Invalid File!!");
        exit;
    }

    if($image_name == -1){
        appRedirect("error","add-blog","Invalid Size or File Size is too large!!");
        exit;
    }

    $data['editor-style01'] = dataReady($data['editor-style01']);

    $blog_id = $bm->addBlog($data, $image_name);

    if($blog_id > 0){
        appRedirect("success","list-blogs","blog Added Successfully");exit;
    } else {
        appRedirect("error","add-blog","Unable to add this blog!!");exit;
    }
}

function editBlog($data, $files){
    global $pm;
    if(!is_admin()) exit;


    $_SESSION['ap-old']['title'] = $_POST['title'];
    $_SESSION['ap-old']['short_description'] = $_POST['short_description'];
    $_SESSION['ap-old']['description'] = $_POST['description'];

    $image_name = '';

    if(!empty($files) && !empty($files['thumbnail']) && !empty($files['thumbnail']['size']) && $files['thumbnail']['size'] > 0){
        $image_name = uploadImage('blog-images', $files, 'thumbnail');
    }

    $pm->editBlog($data, $image_name);

    appRedirect("success","list-blogs","Blog Edited Successfully");
}

?>
blogModal.php
<?php
include_once('Modal.php');
class blogModal extends Modal{
    function __construct($db){
        $this->db = $db;
        $this->datetime = date("Y-m-d H:i:s");
        $this->date = date("Y-m-d");
    }
    public function addBlog($data, $main_image){

        $insert_data = array();
        $insert_data['title'] = $data['blog-name'];
        $insert_data['description'] = $data['editor-style01'];
        $insert_data['short_description'] = $data['short-content'];
        $insert_data['image'] = $main_image;
        $insert_data['created_at'] = date("Y-m-d H:i:s");
        $insert_data['status'] = '1';

        return $this->db->insert("blogs", $insert_data);
    }
    public function toggleBlog($id){
        $qry = $this->db->query("SELECT status FROM blogs WHERE id=$id LIMIT 1");
        if($qry->count > 0){
            if($qry->row->status == '1')
                $this->db->query("UPDATE blogs SET status='0' WHERE id=$id LIMIT 1");
            else
                $this->db->query("UPDATE blogs SET status='1' WHERE id=$id LIMIT 1");
        }
        return true;
    }

    public function getBlogId($id){
        $qry = $this->db->query("SELECT * FROM blogs WHERE id = '$id'");
        if($qry->count > 0){
            return $qry->row;
        } else {
            return false;
        }
    }


        public function editBlog($data, $main_image){

        $update_data = array();
        $update_data['title'] = $data['title'];
        $update_data['short_description'] = $data['short_description'];
        $update_data['description'] = $data['description'];

        if($main_image)
            $update_data['thumbnail'] = $main_image;

        $where = array();
        $where['id'] = $data['id'];

        // pre($update_data, 0);
        // pre($where);

        return $this->db->upDate("blogs", $update_data, $where);
    }


    public function getAllBlogs(){
        $qry = $this->db->query("SELECT id, title, image, created_at, status, short_description FROM blogs ORDER BY id DESC");
        if($qry->count > 0){
            return $qry->rows;
        } else {
            return false;
        }
    }


}

Recommended Answers

All 3 Replies

I’m on my phone about to head to bed so I can’t really look into this too deeply tonight, but I would begin by echo’ing debug statements throughout requestHandler.php to see where the failure is happening.

Have you started the session somewere first?

Also instead of:

$post_query = "SELECT * FROM blogs WHERE id = '$post_id' LIMIT 1";
$post_query_res = mysqli_query($con ,$post_query);

do:

$post_query = "SELECT * FROM blogs WHERE `id`=?";
$post_stmt = $con->stmt_init();
$post_stmt->prepare($post_query);
$post_stmt->bind_param("i", $post_id);
$post_stmt->execute();
$post_query_res = $post_stmt->get_result();
$post_results = $post_query_res->fetch_assoc();
$numRows = $post_query_res->num_rows;

to avoid any dirty extra hands on your database :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.