基于select值的OOP-PHP

基于select值的OOP-PHP,php,forms,oop,post,mysqli,Php,Forms,Oop,Post,Mysqli,我正在PHP中练习OOP,在提交涉及子类的表单数据时遇到问题 我想做的是:根据产品类型(通用、工具或电子)提交表单数据。我担心的是无法提交能够区分不同产品类型的表格 以下是产品类(基类): 这就是我遇到问题的地方: <!DOCTYPE html> <html> <head> <title>Product Entry</title> </head> <body> <select name="pr

我正在PHP中练习OOP,在提交涉及子类的表单数据时遇到问题

我想做的是:根据产品类型(通用、工具或电子)提交表单数据。我担心的是无法提交能够区分不同产品类型的表格

以下是
产品类
(基类):

这就是我遇到问题的地方:

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

产品条目
选择。。。
一般的
工具
数码产品



我会这样做:

  • 将所有计算移到文件顶部
  • 将prodType移动到表单中
  • 我总是显示表单。在一个实例中是编辑,在另一个实例中是创建。但是您需要为“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>
    
    
    产品条目
    
    实际的行为是什么?有任何错误消息吗?@FelisCatus,没有错误消息——当我选择“常规”并按“提交”时,表单会清除,但不会向数据库发送任何内容将您的prodType选择选项放入表单内部。这可能会克服你的问题你在哪里选择“一般”。我是否缺少
    选择
    框?我可以让它正确提交“常规”产品,但无法提交“工具”产品。这就是我试图做的验证:
    if(isset($\u POST['submit'])&&(isset($\u POST['prodType'])=='tools'){//$tool->insertTool();}
    注意:我在创建了提交一般产品的验证(如您上面所建议的)之后尝试这么做,谢谢!在过去的几天里,我一直在“放屁”,这正是我想要做的!
    <!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>
    
    <?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>