Untitled

i have error on updating food, this was the code
mail@pastecode.io avatar
unknown
plain_text
3 years ago
8.5 kB
2
Indexable
Never
<?php
include('partials/menu.php');
require 'functions.php';
?>
<div class="main-content">
    <div class="wrapper">
        <h1>Update food</h1>

        <?php
        if (isset($_SESSION['update'])) {
            echo $_SESSION['update']; //display session
            unset($_SESSION['update']); //remove session
        }
        ?>

        <br><br>
        <?php
        if (isset($_GET["id"])) {
            //get id
            $id = $_GET["id"];
            //create sql
            $sql2 = "SELECT * FROM food WHERE id='$id'";
            //execute
            $res2 = mysqli_query($link, $sql2);

            $row2 = mysqli_fetch_row($res2);
            $current_image = $row2[4];
            $current_category = $row2[5];
        } else {
            //redirect
            header('location:' . SITEURL . '/admin/food.php');
        }
        ?>
        <form action="" method="POST" enctype="multipart/form-data">
            <table class="tbl-full">
                <tr>
                    <td>Title :</td>
                    <td>
                        <input type="text" name="title" placeholder="Food Title" value="<?= $row2[1] ?>">
                    </td>
                </tr>

                <tr>
                    <td>Description :</td>
                    <td>
                        <textarea name="description" id="" cols="30" rows="10" placeholder="Food Description"><?= $row2[2] ?></textarea>
                    </td>
                </tr>

                <tr>
                    <td>Price :</td>
                    <td>
                        <input type="number" name="price" placeholder="Price" value="<?= $row2[3] ?>">
                    </td>
                </tr>

                <tr>
                    <td>Current Image :</td>
                    <td>
                        <?php
                        if ($current_image != "") {
                            //display image 
                        ?>
                            <img width="200px" src="<?php echo SITEURL; ?>/images/food/<?php print_r($row2[4]); ?> ">
                        <?php
                        } else {
                            //display message
                            echo "<div class='error'>Image not Added</div>";
                        }
                        ?>
                    </td>
                </tr>

                <tr>
                    <td>Select Image :</td>
                    <td>
                        <input type="file" name="image">
                    </td>
                </tr>

                <tr>
                    <td>Category :</td>
                    <td>
                        <select name="category" id="">

                            <?php
                            //sql
                            $sql = "SELECT * FROM category WHERE active='Yes'";
                            $res = mysqli_query($link, $sql);
                            $count = mysqli_num_rows($res);
                            if ($count > 0) {

                                while ($row = mysqli_fetch_row($res)) {
                                    $id = $row[0];
                                    $title = $row[1];
                            ?>
                                    <option <?php if ($current_category == $id) {
                                                echo "selected";
                                            } ?>value="<?= $id; ?>"><?= $title; ?></option>

                            <?php
                                }
                            } else {
                                //display we dont have any category
                                echo "<option value='0'>Category Not Available</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td>Featured :</td>
                    <td>
                        <input <?php if ($row2[6] == "Yes") {
                                    echo "checked";
                                } ?> type="radio" name="featured" value="Yes">Yes
                        <input <?php if ($row2[6] == "No") {
                                    echo "checked";
                                } ?> type="radio" name="featured" value="No">No
                    </td>
                </tr>

                <tr>
                    <td>Active :</td>
                    <td>
                        <input <?php if ($row2[7] == "Yes") {
                                    echo "checked";
                                } ?> type="radio" name="active" value="Yes">Yes
                        <input <?php if ($row2[7] == "No") {
                                    echo "checked";
                                } ?> type="radio" name="active" value="No">No
                    </td>
                </tr>

                <tr>
                    <td colspan="2">
                        <input type="hidden" name="current_image" value="<?php print_r($row2[4]); ?>">
                        <input type="hidden" name="id" value="<?= $row2[0] ?>">
                        <input class="btn-secondary" type="submit" name="submit" value="Update Food">
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>
<?php
include('partials/footer.php');

//process the data from the Form
//if (isset($_POST["submit"])) {}

if (isset($_POST['submit'])) {
    // Get data
    $id = $_POST['id'];
    $title = $_POST['title'];
    $description = $_POST['description'];
    $price = $_POST['price'];
    $current_image = $_POST['current_image'];
    $category = $_POST['category'];

    $featured = $_POST['featured'];
    $active = $_POST['active'];

    //update image if selected
    if (isset($_FILES['image']['name'])) {

        $image_name = $_FILES['image']['name'];

        if ($image_name != "") {
            //image available
            //upload image

            //auto rename image
            //get file extension
            $tmp = explode('.', $image_name);
            $ext = end($tmp);

            //rename
            $image_name = "food_name_" . rand(0000, 9999) . '.' . $ext;

            $source_path =  $_FILES['image']['tmp_name'];

            $destination_path = "../images/food/" . $image_name;

            $upload = move_uploaded_file($source_path, $destination_path);

            if ($upload == false) {
                $_SESSION['upload'] = '<div class="error">Image Upload Failed</div>';
                //redirect
                header("location:" . SITEURL . '/admin/food.php');
                //stop proccess
                die();
            }

            //remove current image if available
            if ($current_image != "") {

                $remove_path = "../images/food/" . $current_image;

                $remove = unlink($remove_path);

                if ($remove == false) {
                    //set session message
                    $_SESSION['failed-remove-image'] = " <div class='error'>Failed to remove food Image</div> ";
                    //redirect
                    header("location:" . SITEURL . '/admin/food.php');
                    die();
                }
            }
        } else {

            $image_name = $current_image;
        }
    } else {

        $image_name = $current_image;
    }

    // query insert data
    $sql3 = "UPDATE food SET
            title='$title',
            description='$description',
            price='$price',
            image_name='$image_name',
            category_id=$category,
            featured='$featured',
            active='$active' 
            WHERE id=$id
            ";

    // Execute query
    $res3 = mysqli_query($link, $sql3);

    //cek data add
    if ($res3 == true) {
        //data inserted
        $_SESSION['update'] = '<div class="success">Food Updated Successfully</div>';
        //redirect
        header("location:" . SITEURL . '/admin/food.php');
    } else {
        //data fail to insert
        $_SESSION['update'] = '<div class="error">Food Update Failed</div>';
        //redirect
        header("location:" . SITEURL . '/admin/food.php');
    }
}

?>