1

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

  • 2
    I suggest you a more structured approach, not mixing html rendering with php logic. What happen when you submit the form, do you get any error? – Francesco Gasparetto Feb 19 '20 at 16:55
  • See https://stackoverflow.com/questions/31367098/how-to-submit-0-if-checkbox-is-unchecked-and-submit-1-if-checkbox-is-checked-in – beltouche Feb 19 '20 at 17:15
  • 1
    Since you are using a checkbox, a value will either be sent up if it is checked or no value sent up at all if it is unchecked. Therefore it really doesn't matter what that value sent up is; you just need to determine whether the checkbox was checked (some value was sent up) or not because when it is not checked there won't be any value sent up.. So I would simply say: `$itemSlider = isset($data['item-slider']) ? 1 : 0;` I am not sure though why you would be having a problem with your code when the checkbox is unchecked *unless you are not retrieving and re-displaying the data after an update*. – Booboo Feb 19 '20 at 19:51
  • I looked a bit further: After you have set the value of `$itemSlider` to 1 or 0, there is no reason to be testing the value with the `empty` function. `empty(0)` will be `True`. So what? Whether it is 1 or 0, you want to update the value in the database with the new value. But you are currently not if it is "empty". – Booboo Feb 19 '20 at 20:01

0 Answers0