/home/hdwebsolution/www/ns-interior/admin-pages/Category-image.php
<?php include('site-header.php'); ?>
<?php include('left-nevigation.php'); ?>
<main id="playground">
<?php include('site-top-header.php'); ?>
<script src="ckeditor/ckeditor.js"></script>

<?php 
$product_id = $_GET['pro_id'];
$image_name = isset($_FILES['gal_image']['name']) ? $_FILES['gal_image']['name'] : ''; 
$image_names = $image_name ? time()."_".$image_name : '';

// Insert
if (isset($_REQUEST['Insert'])) {
    $text = mysqli_real_escape_string($link, $_POST['text']);
    if ($image_name == "") {
        echo "<script>alert('Please select an image');</script>";
    } elseif ($text == "") {
        echo "<script>alert('Please enter description text');</script>";
    } else {
        $sqlinsrt = "INSERT INTO tbl_category_image SET pro_id=$product_id, gal_image='$image_names', text='$text'";
        mysqli_query($link, $sqlinsrt);
        move_uploaded_file($_FILES['gal_image']['tmp_name'], "../product-image/".$image_names);
    }
}

// Update
if (isset($_REQUEST['Updt'])) {
    $text = mysqli_real_escape_string($link, $_POST['text']);
    if ($_FILES['gal_image']['name'] == "") { 
        $sqlu = "SELECT * FROM tbl_category_image WHERE gal_id='$_REQUEST[img_id]'";
        $datau = mysqli_query($link, $sqlu);
        $resultu = mysqli_fetch_array($datau);
        $image_names = $resultu['gal_image'];
    }
    $del_image = $_REQUEST['del_image'];
    $sqlinsrt = "UPDATE tbl_category_image SET gal_image='$image_names', text='$text' WHERE gal_id='$_REQUEST[img_id]'";
    mysqli_query($link, $sqlinsrt);

    if ($_FILES['gal_image']['name'] != "") {
        if (move_uploaded_file($_FILES['gal_image']['tmp_name'], "../product-image/".$image_names)) {
            unlink("../product-image/$del_image"); 
        }
    }
}

// Delete
if (!empty($_REQUEST['del_logo_id'])) {
    $del_logo_id = $_REQUEST['del_logo_id'];
    $del_image = $_REQUEST['del_image'];
    $sql = "DELETE FROM tbl_category_image WHERE gal_id='$del_logo_id'";
    if ($data = mysqli_query($link, $sql)) {
        unlink("../product-image/$del_image");
    }
}

$sql = "SELECT * FROM `tbl_category_image` WHERE pro_id=$product_id";
$data = mysqli_query($link, $sql);
$count = mysqli_num_rows($data);
?>     

<section id="page-title" class="row">
   <div class="col-md-8">
      <h1>Category Image</h1>
      <p class="lead">Update your Category Image</strong>.</p>
      <p style="font-weight: bold;font-size:18px;">Category Image Size (1000 x 350) Pixel</p>
      <a href="update-category.php" class="btn btn-primary">Back</a>
   </div>
</section>

<div class="container-fluid">
   <div class="row">
      <?php while($result=mysqli_fetch_array($data)){ ?>
      <form enctype="multipart/form-data" method="post">
         <div class="col-md-6">
            <section class="panel tasks-widget">
               <img src="../product-image/<?php echo $result['gal_image']; ?>" class="img-responsive" style="height:280px; width:100%;">
            </section>
            <div class="text-center"><input type="file" name="gal_image"></div>
            <input type="hidden" name="del_image" value="<?php echo $result['gal_image']; ?>">
            <input type="hidden" name="img_id" value="<?php echo $result['gal_id']; ?>">
            <div style="margin:10px 0;">
               <textarea name="text" id="editor<?php echo $result['gal_id']; ?>" required><?php echo htmlspecialchars($result['text']); ?></textarea>
               <script> CKEDITOR.replace("editor<?php echo $result['gal_id']; ?>"); </script>
            </div>
            <div class="text-center"><button class="btn btn-primary" name="Updt" style="width:100%;">Update</button></div>
      </form>

      <form method="post">
         <input type="hidden" name="del_image" value="<?php echo $result['gal_image']; ?>">
         <input type="hidden" name="del_logo_id" value="<?php echo $result['gal_id']; ?>">
         <div class="text-center"><button class="btn btn-danger" name="del" style="width:100%;">Delete</button></div> 
      </form>
      </div>
      <?php } ?>
   </div>
</div>

<?php if($count < 10){ ?>
<div class="container-fluid" style="margin-top:80px;">
   <div class="row">
      <div class="col-md-4"></div>
      <form method="post" enctype="multipart/form-data">
         <div class="col-md-4">
            <div class="text-center"><input type="file" name="gal_image" class="btn btn-primary" required></div>
            <div style="margin:10px 0;">
               <textarea name="text" id="newEditor" required></textarea>
               <script> CKEDITOR.replace("newEditor"); </script>
            </div>
            <div class="text-center"><button type="submit" name="Insert" class="btn btn-success" style="width:100%;">Add Image + Text</button></div>
         </div>
      </form>
      <div class="col-md-4"></div>
   </div>
</div>
<?php } ?>

<?php include('site-footer.php'); ?>