我的表單中的複選框可以在選中時輸入真值(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>
什麼是第37行? – Bernhard
請不要使用這樣的東西的片段。它也未能顯示正確的語法高亮顯示。 –