基于select值的OOP PHP


OOP PHP based on select values

我正在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>

我确信有一个相当简单的解决方案,但我不再正确地思考这个问题了。有什么建议吗?

我会执行以下操作:

  1. 将所有计算移动到文件顶部
  2. 将prodType移动到表单中
  3. 我一直在展示表格。在一个实例中,它是要编辑的,在另一个实例则是要创建的。但是您需要为"product_id"添加一个隐藏的输入

像这样:

<?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>

注意:你应该使用和学习作曲家。它是自动加载类文件的必备工具。