我创建了一个允许我添加产品的应用程序,在产品create_product.php页面上,我可以查看一个框,告诉我们产品是否发布。 (真或假(1或0))
当我在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'];
$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";
?>
这是带有update()函数的产品类..
<?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;
}
}
答案 0 :(得分: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>
在浏览器中打开update_page时,$ _ POST数组为空。我不明白为什么对于其他字段你正在使用$ product-&gt; FIELD样式,之后使用raw $ _POST进行相同类型的操作。