2017-06-27 69 views
-1

我的表單中的複選框可以在選中時輸入真值(1),否則該列的值爲false(0)..但是,當我運行沒有複選框託運形式它給我這個錯誤 -錯誤複選框沒有在php mysql表格中檢查

「通知:未定義指數:用C發佈:\ XAMPP \ htdocs中\ phpoop \ create_product.php上線37」

這裏創建產品的產品類(僅包括髮布變量)

class Product{ 

    // database connection and table name 
    private $conn; 
    private $table_name = "products"; 

    // object properties 
     public $publish; 


    public function __construct($db){ 
     $this->conn = $db; 
    } 

    // create product 
    function create(){ 

     //write query 
     $query = "INSERT INTO 
        " . $this->table_name . " 
       SET 
        publish=:publish"; 

     $stmt = $this->conn->prepare($query); 

     // posted values 
     $this->publish=htmlspecialchars(strip_tags($this->publish)); 

     // to get time-stamp for 'created' field 
     $this->timestamp = date('Y-m-d H:i:s'); 
     $this->timestamp2 = date('Y-m-d H:i:s'); 

     // bind values 
     $stmt->bindParam(":publish", $this->publish); 


     if ($stmt->execute()){ 
      return true;  

     }else{ 
      return false; 
     } 

    } 

而這正是形式被定位用於創建產品

// if the form was submitted 
if ($_POST){ 

    // set product property values 
    $product->publish = $_POST['publish']; 

    if(isset($_POST['publish'])){ 
    $published = $_POST['publish']; 
    } 
    else{ 
     $published = 0; 
    } 

    // create the product 
    if($product->create()){ 
     echo "<div class='alert alert-success'>Product was created.</div>"; 
    } 


} 

?> 

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> 
    <input type="checkbox" name="publish" value="1" class="form-control" <?php if(isset($_POST['publish'])) echo "checked='checked'";?>/> 
    <button type="submit" class="btn btn-primary">Create Product</button> 
</form> 
+0

什麼是第37行? – Bernhard

+0

請不要使用這樣的東西的片段。它也未能顯示正確的語法高亮顯示。 –

回答

0

在這裏,我們檢查,如果試圖使用它之前就存在價值:(之前

if(isset($_POST['publish'])){ 
    $published = $_POST['publish']; 
} 
else{ 
    $published = 0; 
} 

但在這裏它)你不是:

$product->publish = $_POST['publish']; 

只是包括在相同的邏輯,以檢查它是否存在之前嘗試使用它:

if(isset($_POST['publish'])){ 
    $published = $_POST['publish']; 
    $product->publish = $_POST['publish']; 
} 
else{ 
    $published = 0; 
    $product->publish = 0; 
} 
+0

工作!非常感謝。 – DesignStuff

+0

我將如何實現這個更新產品頁面? – DesignStuff