mercredi 28 juin 2017

Checkbox unchecked when updating product - php myadmin

I've created an application that allows me to add products, and on the product create_product.php page I am able to check a box telling us whether the product is publish or not. (true or false (1 or 0))

When I go to update the product on update_product.php the checkbox isn't checked even if it was before. I can't seem to get the checkbox to be checked when I go to update the product.

Here is the update_product.php

<?php
// get ID of the product to be edited
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: missing ID.');
 
// include database and object files
include_once 'config/database.php';
include_once 'objects/product.php';
include_once 'objects/category.php';
 
// get database connection
$database = new Database();
$db = $database->getConnection();
 
// prepare objects
$product = new Product($db);
$category = new Category($db);
 
// set ID property of product to be edited
$product->id = $id;
 
// read the details of product to be edited
$product->readOne();


$page_title = "Update {$product->name}";
include_once "header.php";
 
echo "<div class='right-button-margin'>";
    echo "<a href='index.php' class='btn btn-default pull-right'>Read Products</a>";
echo "</div>";

// if the form was submitted
if($_POST){
 
    // set product property values
    $product->name = $_POST['name'];
        $product->family = $_POST['family'];
    //$product->number = $_POST['number'];
    $product->description = $_POST['description'];
        $product->ext_description = $_POST['ext_description'];
    //$product->category_id = $_POST['category_id'];
        
        if(isset($_POST['publish'])){
    $published = $_POST['publish'];
        $product->publish = $_POST['publish'];
        }
        else{
                //$publish is not checked and value=0
                $published = 0;
                $product->publish = 0;
        }
 
    // update the product
    if($product->update()){
        echo "<div class='alert alert-success alert-dismissable'>";
            echo "Product was updated.";
        echo "</div>";
    }
 
    // if unable to update the product, tell the user
    else{
        echo "<div class='alert alert-danger alert-dismissable'>";
            echo "Unable to update product.";
        echo "</div>";
    }
}


?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
    <table class='table table-hover table-responsive table-bordered'>
 
        <tr>
            <td>Name</td>
            <td><input type='text' name='name' value='<?php echo $product->name; ?>' class='form-control' /></td>
        </tr>
        <td>Product Family</td>
                <td><select class="selectpicker" name='family'>
                        <option selected="selected" value="<?php echo $product->family; ?>">Selected - <?php echo $product->family; ?></option>
                                <option value="COM Express">COM Express</option>
                                <option value="NVIDIA Jetson TX2/TX1">NVIDIA Jetson TX2/TX1</option>
                                <option value="PC/104">PC/104</option>
                                <option value="SMARC">SMARC</option>
                                </select>
                </td>
        
<!--        
        <tr>
            <td>Category</td>
            <td>
        //     <?php
                //              $stmt = $category->read();

                                // put them in a select drop-down
                //              echo "<select class='form-control' name='category_id'>";

                //                      echo "<option>Please select...</option>";
                //                      while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
                //                              extract($row_category);

                                                // current category of the product must be selected
                //                              if($product->category_id==$id){
                //                                      echo "<option value='$id' selected>";
                //                              }else{
                //                                      echo "<option value='$id'>";
                //                              }
                //
                //                              echo "$name</option>";
                //                      }
                //              echo "</select>";
                                ?>
            </td>
        </tr>
 -->
        <tr>
            <td>Part Number</td>
            <td><?php echo $product->id; ?></td>
        </tr>
        
        <tr>
            <td>External Description</td>
            <td><textarea name='ext_description' class='form-control'><?php echo $product->ext_description; ?></textarea></td>
        </tr>
 
        <tr>
            <td>Interal Description</td>
            <td><textarea name='description' class='form-control'><?php echo $product->description; ?></textarea></td>
        </tr>
        
         <tr>
            <td>Publish</td>
            <td><input type="checkbox" name="publish"  value="1" class="form-control" <?php if(isset($_POST['publish'])) echo "checked='checked'";?>/></td>
        </tr>
 
        <tr>
            <td></td>
            <td>
                <button type="submit" class="btn btn-primary">Update</button>
            </td>
        </tr>
 
    </table>
</form>


<?php

 
// set page footer
include_once "footer.php";
?>

and here is the product class with the update() function..

<?php
class Product{
 
    // database connection and table name
    private $conn;
    private $table_name = "products";
        private $table2_name = "deleted_products";
 
    // object properties
    public $id;
    public $name;
        public $family;
    public $number;
        public $description;
    public $ext_description;
        public $publish;
    public $category_id;
        public $timestamp;
    public $timestamp2;
 
    public function __construct($db){
        $this->conn = $db;
    }
    
    function update(){
 
    $query = "UPDATE
                " . $this->table_name . "
            SET
                name = :name,
                number = :number,
                                family = :family,
                                ext_description = :ext_description,
                description = :description,
                category_id  = :category_id,
                                modified  = :modified,
                                publish = :publish
                                
            WHERE
                id = :id";
 
    $stmt = $this->conn->prepare($query);
 
    // posted values
    $this->name=htmlspecialchars(strip_tags($this->name));
    $this->number=htmlspecialchars(strip_tags($this->number));
        $this->family=htmlspecialchars(strip_tags($this->family));
        $this->ext_description=htmlspecialchars(strip_tags($this->ext_description));
    $this->description=htmlspecialchars(strip_tags($this->description));
    $this->category_id=htmlspecialchars(strip_tags($this->category_id));
    $this->id=htmlspecialchars(strip_tags($this->id));
        $this->publish=htmlspecialchars(strip_tags($this->publish));
        
        //get times of last updated and created date
        $this->timestamp = date('Y-m-d H:i:s');
        $this->timestamp2 = date('Y-m-d H:i:s');
 
    // bind parameters
        
    $stmt->bindParam(':name', $this->name);
    $stmt->bindParam(':number', $this->number);
        $stmt->bindParam(':family', $this->family);
        $stmt->bindParam(':ext_description', $this->ext_description);
    $stmt->bindParam(':description', $this->description);
    $stmt->bindParam(':category_id', $this->category_id);
    $stmt->bindParam(':id', $this->id);
        $stmt->bindParam(":modified", $this->timestamp2);
        $stmt->bindParam(":publish", $this->publish);
 
    // execute the query
    if($stmt->execute()){
        return true;
    }
 
    return false;
     
        }
}
    
    



Aucun commentaire:

Enregistrer un commentaire