Mini Shell

Direktori : /home/haworld/www/admin/
Upload File :
Current File : /home/haworld/www/admin/category-add.php

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include('include/head_admin.php');
    $page_id = '3';

    $name = '';
    $image = '';
    $image_alt_tag = '';
    $url = '';

    if (isset($_GET['id']) && $_GET['id'] != '') {
        $id = get_safe_value($con, $_GET['id']);
        $image_required = '';
        $res = mysqli_query($con, "select * from category where id='$id'");
        $check = mysqli_num_rows($res);
        if ($check > 0) {
            $row = mysqli_fetch_assoc($res);
            $name = $row['name'];
            $image = $row['image'];
            $image_alt_tag = $row['image_alt_tag'];
            $url = $row['url'];
        } else {
            header('location:category-all.php');
            die();
        }
    }

    if (isset($_POST['submit_category'])) {
        // prx($_POST);
        $name = get_safe_value($con, $_POST['name']);
        $image_alt_tag = get_safe_value($con, $_POST['image_alt_tag']);
        $url = generate_seo_friendly_title($name);

        if (isset($_GET['id']) && $_GET['id'] == 0) {
            if ($_FILES['image']['type'] != 'image/png' && $_FILES['image']['type'] != 'image/jpg' && $_FILES['image']['type'] != 'image/jpeg' && $_FILES['image']['type'] != 'image/webp') {
                $msg = "Please select only png, jpg, webp and jpeg image format";
            }
        } else {
            if ($_FILES['image']['type'] != '') {
                if ($_FILES['image']['type'] != 'image/png' && $_FILES['image']['type'] != 'image/jpg' && $_FILES['image']['type'] != 'image/jpeg' && $_FILES['image']['type'] != 'image/jpeg') {
                    $msg = "Please select only png, jpg, webp and jpeg image format";
                }
            }
        }

        $msg = "";

        if ($msg == '') {
            if (isset($_GET['id']) && $_GET['id'] != '') {
                if ($_FILES['image']['name'] != '') {
                    $image =  $_FILES['image']['name'];
                    move_uploaded_file($_FILES['image']['tmp_name'], "../media/category/" . $image);
                    mysqli_query($con, "UPDATE `category` SET `name`='$name',`image`='$image',`image_alt_tag`='$image_alt_tag', `url`='$url' WHERE `id`='$id'");
                } else {
                    $update_query = "UPDATE `category` SET `name`='$name', `image`='$image',`image_alt_tag`='$image_alt_tag',`url`='$url' WHERE `id`='$id'";

                    mysqli_query($con, $update_query);
                }
            } else {
                $image = rand(111111111, 999999999) . '_' . $_FILES['image']['name'];
                move_uploaded_file($_FILES['image']['tmp_name'], "../media/category/" . $image);
                $insert_query = "INSERT INTO `category`(`name`, `image`,`image_alt_tag`,`url`) 
            VALUES ('$name','$image','$image_alt_tag','$url')";
                mysqli_query($con, $insert_query);
                print_r($insert_query);
            }
            header('location:category-all.php');
            die();
        }
    }
    function generate_seo_friendly_title($title)
    {
        // Convert the title to lowercase
        $title = strtolower($title);

        // Replace spaces with dashes
        $title = str_replace(' ', '-', $title);

        // Remove special characters
        $title = preg_replace('/[^A-Za-z0-9\-]/', '', $title);

        return $title;
    }

    ?>
</head>

<body>
    <!--== MAIN CONTRAINER ==-->
    <?php include('include/header_admin.php'); ?>

    <!--== BODY CONTNAINER ==-->
    <div class="container-fluid sb2">
        <div class="row">

            <?php include('include/sidebar_admin.php'); ?>

            <div class="sb2-2">
                <div class="sb2-2-2">
                    <ul>
                        <li><a href="index.php"><i class="fa fa-home" aria-hidden="true"></i> Home</a>
                        </li>
                        <li class="active-bre"><a href="#"> Add New Post</a>
                        </li>
                        <!-- <li class="page-back"><a href="index.php"><i class="fa fa-backward" aria-hidden="true"></i> Back</a>
                        </li> -->
                    </ul>
                </div>
                <div class="sb2-2-add-category sb2-2-1">
                    <div class="box-inn-sp">
                        <div class="inn-title">
                            <h4>Add New Post</h4>
                            <!-- <p>Airtport Hotels The Right Way To Start A Short Break Holiday</p> -->
                        </div>
                        <div class="bor">
                            <form method="post" enctype="multipart/form-data">
                                <div class="row">
                                    <div class="input-field col s12">
                                        <input id="name" type="text" name="name" class="validate" value="<?php echo $name ?>" required>
                                        <label for="name">Name</label>
                                    </div>
                                    <!-- <div class="input-field col s12">
                                        <input id="url" type="text" name="url" class="validate" value="<?php echo $url ?>" required>
                                        <label for="url"> Url</label>
                                    </div> -->
                                    <!-- <div class="input-field col s12">
                                       <div class="file-field">
                                           <div class="btn">
                                               <span>File</span>
                                               <input type="file" name="image" value="<?php echo $image ?>">
                                           </div>
                                           <div class="file-path-wrapper">
                                               <input class="file-path validate" type="text" name="image" placeholder="Upload category image" value="<?php echo $image ?>">
                                           </div>
                                       </div>
                                        <?php
                                        if ($image != '') {
                                            echo "<a target='_blank' href='" . "../media/category/" . $image . "'><img width='150px' src='" . "../media/category/" . $image . "'/></a>";
                                        }
                                        ?>
                                    </div>
                                    <div class="input-field col s12">
                                       <input id="image_alt_tag" type="text" name="image_alt_tag" class="validate" value="<?php echo $image_alt_tag ?>" required>
                                       <label for="image_alt_tag">Image Alt Text</label>
                                    </div> -->

                                </div>
                                <div class="row">
                                    <div class="input-field col s12">
                                        <input type="submit" name="submit_category" class="waves-effect waves-light btn-large" value="Submit">
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php include('include/footer_admin.php'); ?>

    <?php include('include/foot_admin.php'); ?>


</body>

</html>