基于选择值的 OOP PHP
Posted
技术标签:
【中文标题】基于选择值的 OOP PHP【英文标题】:OOP PHP based on select values 【发布时间】:2016-02-18 09:03:20 【问题描述】:我正在使用 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>
我确信有一个相当简单的解决方案,但我不再正确地考虑这个问题了 -_-。有什么建议吗?
【问题讨论】:
实际行为是什么?有错误提示吗? @FelisCatus,没有错误消息--当我选择“常规”并按提交时,表单清除但没有任何内容发送到数据库 将您的 prodType 选择选项放入表单中。 Thia 可能会解决您的问题 在哪里选择“一般”。我错过了select
框吗?
【参考方案1】:
我会做以下事情:
-
将所有计算移至文件顶部。
将您的 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>
注意:您应该使用和学习composer。它是自动加载类文件的必备工具。
【讨论】:
我可以让它正确提交“一般”产品,但不能提交“工具”产品。这是我正在尝试进行的验证:if (isset($_POST['submit']) && (isset($_POST['prodType']) == 'tools')) //$tool->insertTool();
注意:在我创建了用于提交一般产品的验证(如您上面建议的)之后,我正在尝试这样做
谢谢!过去几天我一直在“放屁”,而这正是我想要做的!以上是关于基于选择值的 OOP PHP的主要内容,如果未能解决你的问题,请参考以下文章
无法选择最后插入的 id 为 [Mysql, php pdo oop] [重复] 的行