我在PHP中使用OOP进行一些练习,并且在提交涉及子类的表单数据时遇到了问题。
我要做的是:根据产品的类型提交表单数据(通用,工具或电子)。我担心的是无法提交可以区分不同产品类型的表单。
这里是Product Class
(基类):
<?php
require_once('connectvars.php');
// Base class!!
class Product {
// Inheritable properties
protected $title;
protected $description;
protected $price;
// Getters
public function getTitle() {
return $this->title;
}
public function getDescription() {
return $this->description;
}
public function getPrice() {
return $this->price;
}
// Setters
public function setTitle($title) {
$this->title = $title;
}
public function setDescription($description) {
$this->description = $description;
}
public function setPrice($price) {
$this->price = $price;
}
public function insertProduct() {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
$query = "INSERT INTO addedProducts VALUES (0,'$this->title', '$this->description', '$this->price', '', '', '')";
mysqli_query($dbc, $query)
or die("Error adding to database");
mysqli_close($dbc);
}
}
?>
这是我创建的一个名为Tools
的子类:
<?php
require_once('connectvars.php');
require_once('Product.php');
class Tools extends Product {
// Defined properties specific to Tools class
private $shipper;
private $weight;
// Getters
public function getShipper() {
return $this->shipper;
}
public function getWeight() {
return $this->weight;
}
// Setters
public function setShipper($shipper) {
$this->shipper = $shipper;
}
public function setWeight($weight) {
$this->weight = $weight;
}
public function insertTool() {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
$query = "INSERT INTO addedProducts VALUES (0,'$this->title', '$this->description', '$this->price', '$this->shipper', '$this->weight', '')";
mysqli_query($dbc, $query)
or die("Error adding to database");
mysqli_close($dbc);
}
}
?>
这是我遇到问题的地方:
<!DOCTYPE html>
<html>
<head>
<title>Product Entry</title>
</head>
<body>
<select name="prodType" id="prodType">
<option value="" selected="selected">Select...</option>
<option value="general">General</option>
<option value="tools">Tools</option>
<option value="electronics">Electronics</option>
</select>
<br/><br/>
<?php
//require_once('connectvars.php');
require_once('Product.php');
require_once('Electronics.php');
require_once('Tools.php');
$product = new Product();
$tool = new Tools();
$electronic = new Electronics();
if (isset($_POST['submit']) && (isset($_POST['prodType']) == 'general')) {
$product_form = false;
$product->setTitle($_POST['title']);
$product->setDescription($_POST['description']);
$product->setPrice($_POST['price']);
$product->insertProduct();
/*$tool->setTitle($_POST['title']);
$tool->setDescription($_POST['description']);
$tool->setPrice($_POST['price']);
$tool->setShipper($_POST['shipper']);
$tool->setWeight($_POST['weight']);
if (!empty($tool->getTitle()) && !empty($tool->getDescription()) && is_numeric($tool->getPrice()) && !empty($tool->getShipper()) && !empty($tool- >getWeight())) {
echo 'Tool submitted <br/>';
//echo '<a href="addProduct.php">Go Back</a>';
$tool->insertTool();
}
} else {
$product_form = true;
}
if ($product_form) {
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<label for="title"><strong>Product Title</strong></label>
<br/>
<input type="text" id="title" name="title" value="<?php echo $product->getTitle();?>"/>
<br/><br/>
<label for="description"><strong>Description</strong></label>
<br/>
<input type="text" id="description" name="description" value="<?php echo $product->getDescription();?>"/>
<br/><br/>
<label for="price"><strong>Price</strong></label>
<br/>
<input type="text" id="price" name="price" value="<?php echo $product->getPrice();?>"/>
<br/><br/>
<!--For Tools -->
<label for="shipper"><strong>Shipper Info</strong></label>
<br/>
<select name="shipper" id="shipper">
<option value="none" selected="selected">--</option>
<option value="usps">USPS</option>
<option value="fedex">FedEx</option>
<option value="ups">UPS</option>
</select>
<br/><br/>
<label for="weight"><strong>Weight</strong></label>
<br/>
<input type="text" id="weight" name="weight" value="<?php echo $tool->getWeight();?>"/>
<br/><br/>
<!--For Electronics -->
<label for="recyclable"><strong>Recyclable?</strong></label>
<br/>
<select name="recyclable" id="recyclable">
<option value="none" selected="selected">--</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br/><br/>
<input type="submit" id="submit" name="submit" value="Submit Product"/>
</form>
<?php
}
?>
</body>
</html>
我确信这是一个相当直接的解决方案,但我不再正确地考虑这个问题-_-了。有什么建议吗?
答案 0 :(得分:2)
我会做以下事情:
像这样:
<?php
require_once('Product.php');
require_once('Electronics.php');
require_once('Tools.php');
$product = new Product();
$tool = new Tools();
$electronic = new Electronics();
if (isset($_POST['submit'])){
$prodType = $_POST['prodType'];
if($prodType == 'general') {
$product_form = false;
$product->setTitle($_POST['title']);
$product->setDescription($_POST['description']);
$product->setPrice($_POST['price']);
$product->insertProduct();
} else if($prodType == 'tools') {
} else if ($prodType == 'elecronics') {
} else {
// echo this message in the form.
$msg = 'Invalid product type';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Product Entry</title>
</head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="prodType" id="prodType">
<option value="" selected="selected">Select...</option>
<option value="general">General</option>
<option value="tools">Tools</option>
<option value="electronics">Electronics</option>
</select>
<br/><br/>
<label for="title"><strong>Product Title</strong></label>
<br/>
<input type="text" id="title" name="title" value="<?php echo $product->getTitle();?>"/>
<br/><br/>
<label for="description"><strong>Description</strong></label>
<br/>
<input type="text" id="description" name="description" value="<?php echo $product->getDescription();?>"/>
<br/><br/>
<label for="price"><strong>Price</strong></label>
<br/>
<input type="text" id="price" name="price" value="<?php echo $product->getPrice();?>"/>
<br/><br/>
<!--For Tools -->
<label for="shipper"><strong>Shipper Info</strong></label>
<br/>
<select name="shipper" id="shipper">
<option value="none" selected="selected">--</option>
<option value="usps">USPS</option>
<option value="fedex">FedEx</option>
<option value="ups">UPS</option>
</select>
<br/><br/>
<label for="weight"><strong>Weight</strong></label>
<br/>
<input type="text" id="weight" name="weight" value="<?php echo $tool->getWeight();?>"/>
<br/><br/>
<!--For Electronics -->
<label for="recyclable"><strong>Recyclable?</strong></label>
<br/>
<select name="recyclable" id="recyclable">
<option value="none" selected="selected">--</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br/><br/>
<input type="submit" id="submit" name="submit" value="Submit Product"/>
</form>
</body>
</html>
注意:您应该使用并学习composer。它是自动加载类文件的必备工具。