mercredi 19 février 2020

Checkbox value not updating in php form

I want to update my checkbox value correctly. If it was checked, then 1 is stored in database otherwise 0 stored in database. I can update the value from 0 to 1 but I cannot update the value from 1 to 0. I really have no idea how to fix this.

Here is my code

productedit.php

<?php include("inc/header.php");?>
<?php include("inc/sidebar.php");?>
<?php include("../classes/Product.php");?>
<?php
    if(!isset($_GET['proid']) || $_GET['proid'] == NULL){
        echo "<script>window.location = '../404.php'; </script>";  
    } else{
        $id = $_GET['proid']; 
    }
    $pd = new Product();
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){
        $id = $_POST['id'];
        $updateProduct = $pd->productUpdate($_POST,$_FILES,$id);
    }
?>
<br>
<br>
<br>
<br>
<br>
<div class="container mt-50">
    <div class="row">
        <div class="col-2">
        </div>
        <div class="col-md-10">
            <?php
                if(isset($updateProduct)){
                   echo $updateProduct;
                }
            ?>  
            <?php
                $getPro = $pd->getProById($id);
                if($getPro){
                    while($value = $getPro->fetch_assoc()){
            ?>   
            <form action="" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="exampleFormControlInput1">Product Name</label>
                    <input type="Name" name="productName" value="<?php echo $value['productName'];?>" class="form-control" id="exampleFormControlInput1" placeholder="Product name">
                </div>

                <div class="form-group">
                    <label for="exampleFormControlTextarea1">Description</label>
                    <textarea name="body" id="user-message" class="form-control" rows="10" placeholder="Enter your Message"> <?php echo $value['body'];?></textarea>
                </div>
                <div class="form-group">
                    <label for="exampleFormControlTextarea1">Price</label>
                    <input type="text" name="price" value="<?php echo $value['price'];?>" class="form-control" id="exampleFormControlInput1"></input>
                </div>
                <div class="form-group">
                    <label for="exampleFormControlTextarea1">Item Slider</label>
                    <input type="checkbox" name="item-slider" id="exampleFormControlInput1" value="1" <?php echo ($value['itemSlider']==1 ? 'checked' : '');?>></input>
                </div>
                <div class="form-group">
                    <label for="exampleFormControlInput1">Product Type</label>
                    <select id="select" name="type">
                        <option>Select Type</option>
                        <?php
                            if($value['type'] == 0){
                        ?>
                            <option selected = "selected" value="0">Featured</option>
                            <option value="1">General</option>
                        <?php        
                            } else {
                        ?>
                        <option value="0">Featured</option>
                        <option selected = "selected" value="1">General</option>
                        <?php        
                            } 
                        ?>
                    </select>
                </div>
                <div class="form-group">
                    <label for="exampleFormControlFile1">Upload a image:</label>
                    <img src="<?php echo $value['image']; ?>" height="80px" width="200px"/>
                    <input type="file" name="image" class="form-control-file" id="exampleFormControlFile1">
                </div>
                <input type="submit" name="submit" class="btn btn-outline-success btn-block" Value="Update"/>
                <input type="hidden" id="i" name="id" value="<?php echo $id;?>">
            </form> 
            <?php 
                    }
                }
            ?>
        </div>
    </div>
</div>
<?php include("inc/footer.php");?>

Product.php

<?php 
    $filepath = realpath(dirname(__FILE__));
    include_once($filepath."/../lib/Database.php");
    include_once($filepath."/../helpers/Format.php");
?>
<?php
    class Product {
        private $db;
        private $fm;
        public function __construct() {
            $this->db = new Database();
            $this->fm = new Format();
        }

public function productUpdate($data,$file,$id){
            $productName = mysqli_real_escape_string($this->db->link , $data['productName']);
            $catId = mysqli_real_escape_string($this->db->link , $data['catId']);
            $body = mysqli_real_escape_string($this->db->link , $data['body']);
            $price = mysqli_real_escape_string($this->db->link , $data['price']);
            if(!empty($data['item-slider'])){
                $itemSlider = mysqli_real_escape_string($this->db->link , $data['item-slider']);
            } else {
                $itemSlider = 0;
            }
            $type = mysqli_real_escape_string($this->db->link , $data['type']);

            $permited  = array('jpg', 'jpeg', 'png', 'gif');
            $file_name = $file['image']['name'];
            $file_size = $file['image']['size'];
            $file_temp = $file['image']['tmp_name'];

            $div = explode('.', $file_name);
            $file_ext = strtolower(end($div));
            $unique_image = substr(md5(time()), 0, 10).'.'.$file_ext;
            $uploaded_image = "uploads/".$unique_image;

            if($productName == "" || $body == "" || $price == "" || $type == "" ){
                $msg = "<span class='error'>Fields must not be empty !</span>";
                return $msg;
            } else {
                if(!empty($file_name)) {
                    if ($file_size >1048567) {
                        echo "<span class='error'>Image Size should be less then 1MB!</span>";
                    } else if (in_array($file_ext, $permited) === false) {
                        echo "<span class='error'>You can upload only:-".implode(', ', $permited)."</span>";
                    } else{
                        move_uploaded_file($file_temp, $uploaded_image);
                        if(!empty($itemSlider)){
                            $query = "UPDATE product SET productName = '$productName',body = '$body',price = '$price',image = '$uploaded_image',itemSlider = '$itemSlider',type = '$type' WHERE productId = '$id'";
                        } else {
                           $query = "UPDATE product SET productName = '$productName',body = '$body',price = '$price',image = '$uploaded_image',type = '$type' WHERE productId = '$id'"; 
                        }

                        $updated_row = $this->db->update($query);
                        if($updated_row){
                            $msg = "<span class='success'>Product Updated Successfully</span>";
                            return $msg;
                        } else {
                            $msg = "<span class='error'>Product Not Updated.</span>";
                            return $msg;
                        }
                    } 
                } else {
                    if(!empty($itemSlider)){
                        $query = "UPDATE product SET productName = '$productName',body = '$body',price = '$price',itemSlider = '$itemSlider',type = '$type' WHERE productId = '$id'";
                    } else {
                        $query = "UPDATE product SET productName = '$productName',body = '$body',price = '$price',type = '$type' WHERE productId = '$id'";
                    }

                    $updated_row = $this->db->update($query);
                    if($updated_row){
                        $msg = "<span class='success'>Product Updated Successfully</span>";
                        return $msg;
                    } else {
                        $msg = "<span class='error'>Product Not Updated.</span>";
                        return $msg;
                    }
                }
            }
        }
}
?>

Suggest me where I went wrong or guide me other way of doing. Many Thanks




Aucun commentaire:

Enregistrer un commentaire