2017-06-28 35 views
0

我創建了一個允許我添加產品的應用程序,並且在產品create_product.php頁面上,我可以選中一個框來告訴我們產品是否已發佈。 (真或假(1或0))更新產品時取消選中複選框 - php myadmin

當我更新update_product.php上的產品時,即使是之前的複選框也不會被選中。當我去更新產品時,我似乎無法得到複選框。

這裏是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']; 
 
\t $product->family = $_POST['family']; 
 
    //$product->number = $_POST['number']; 
 
    $product->description = $_POST['description']; 
 
\t $product->ext_description = $_POST['ext_description']; 
 
    //$product->category_id = $_POST['category_id']; 
 
\t 
 
\t if(isset($_POST['publish'])){ 
 
    $published = $_POST['publish']; 
 
\t $product->publish = $_POST['publish']; 
 
\t } 
 
\t else{ 
 
\t \t //$publish is not checked and value=0 
 
\t \t $published = 0; 
 
\t \t $product->publish = 0; 
 
\t } 
 
    
 
    // 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> 
 
     \t \t <td><select class="selectpicker" name='family'> 
 
     \t \t \t <option selected="selected" value="<?php echo $product->family; ?>">Selected - <?php echo $product->family; ?></option> 
 
\t \t \t \t <option value="COM Express">COM Express</option> 
 
\t \t \t \t <option value="NVIDIA Jetson TX2/TX1">NVIDIA Jetson TX2/TX1</option> 
 
\t \t \t \t <option value="PC/104">PC/104</option> 
 
\t \t \t \t <option value="SMARC">SMARC</option> 
 
\t \t \t \t </select> 
 
     \t \t </td> 
 
     
 
<!--   
 
     <tr> 
 
      <td>Category</td> 
 
      <td> 
 
     //  <?php 
 
\t \t // \t \t $stmt = $category->read(); 
 

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

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

 
\t \t \t \t \t \t // current category of the product must be selected 
 
\t \t // \t \t \t \t if($product->category_id==$id){ 
 
\t \t // \t \t \t \t \t echo "<option value='$id' selected>"; 
 
\t \t // \t \t \t \t }else{ 
 
\t \t // \t \t \t \t \t echo "<option value='$id'>"; 
 
\t \t // \t \t \t \t } 
 
\t \t // 
 
\t \t // \t \t \t \t echo "$name</option>"; 
 
\t \t // \t \t \t } 
 
\t \t // \t \t echo "</select>"; 
 
\t \t \t \t ?> 
 
      </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"; 
 
?>

,這裏是產品類與update()函數..

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

回答

0

糾正我,如果我錯了。您在create_product頁面上創建了產品,然後您想要打開update_product頁面並查看選中的複選框?

如果是這樣,那麼你的代碼是不正確的。

<td><input type="checkbox" name="publish" value="1" class="form-control" <?php if(isset($_POST['publish'])) echo "checked='checked'";?>/></td> 

$ _POST數組在瀏覽器中打開update_page時爲空。我不明白爲什麼其他字段使用$ product-> FIELD樣式,之後使用raw $ _POST作爲相同類型的操作。

+0

這是一個很好的問題。我最初爲應用程序使用了FIELD風格,但無法使用複選框工作,所以我使用了$ _POST選項。你知道我能如何使用FIELD風格嗎? **我確實設法讓它工作**通過用這個代替你上面提到的內容.. ' publish ==」1「)echo」checked ='checked'「;?> />' – DesignStuff

+0

對不起,我不明白。你的意思是「但是無法使用複選框,所以我使用了$ _POST選項」。但是接下來你會說「我設法通過替換你所提到的來解決問題」。 它是否適用於$ product->發佈與否? –

+0

是的。對不起,我以爲你在談論表格上的if和else。 – DesignStuff