[英]Simple insert in PHP5
我正在尝试了解oop php
最佳实践,并且很难调试,我知道这可能是一个重复的问题,我希望不会得到很多反对票,所以让我们解决问题:
我在Products.php
有一个Product
类
class Product {
public $id;
public $name;
public $price;
public $description;
function __construct($id, $name, $price, $description)
{
$this->$id = $id;
$this->$name = $name;
$this->$price = $price;
$this->$description = $description;
}
}
ProductsManager.php
的ProductManger
类
include('Products.php');
include('config.php');
class ProductManager {
function addProduct($conn,Products $p)
{
$query="INSERT INTO `products`( `name`, `description`, `price`) VALUES ('".$name."','".$price."','".$description."')";
$c->exec($query);
}
}
config.php
一个config
类
class config {
function connect() {
$serverName="localhost";
$dbName="phppoo";
$user="root";
$pass="";
return $conn = new PDO("mysql:host=$serverName;dbname=$dbName",$user,$pass);
}
}
//to be clear i think there are no issue related to connection
我提交表单的 Html 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="AddProduct.php" method="POST">
<table border="3">
<tr>
<td>Reference<td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Nom<td>
<td><input type="text" name="desc"></td>
</tr>
<tr>
<td>date de création <td>
<td><input type="number" name="price"></td>
</tr>
<tr>
<td> <td>
<td><input type="submit" name="ajouter" value="ajouter"></td>
</tr>
</form>
</table>
</body>
</html>
最后是AddProduct.php
在这里我实例化产品经理并发送数据以便我可以执行query
include('ProductsManager.php');
$co = new config();
$c=$co->connect();
if(isset($_POST['name']) and isset($_POST['desc'])and isset($_POST['price']) ) {
$p=new ProductManager($_POST['name'],$_POST['desc'],$_POST['price']);
$p->addProduct($c,$p);
}
现在,如果我尝试从html
页面提交form
,我会重定向到一个空白页面AddProduct.php
,这是正常的,因为我没有返回值。
这里的问题是,我没有将提交的新值插入到我的Database
。
感谢任何帮助,PS:如果您认为这不是最佳实践,请提及更好的方法或教程。 谢谢
像这样修改插入语句。 因为您可以通过引用访问产品属性。
$query="INSERT INTO `products`( `name`, `description`, `price`) VALUES
('".$p['name']."','".$p['description']."','".$p['price']."')";
要修改类中的属性,请执行以下操作:
$this->price = $price;
不是这个:
$this->$price = $price;
上面所做的是根据 $price 的值尝试属性。 所以如果 price 是5.0
它将寻找$this->5.0
的属性。
您更新后的课程如下所示:
class Product {
public $id;
public $name;
public $price;
public $description;
function __construct($id, $name, $price, $description)
{
$this->id = $id;
$this->name = $name;
$this->price = $price;
$this->description = $description;
}
}
您的添加产品方法如下所示:
function addProduct($conn,Products $p) {
$query="INSERT INTO `products`( `name`, `description`, `price`) VALUES ('".$p->name."','".$p->description."','".$p->price."')";
$conn->exec($query);
}
$p
变量上的属性未被访问,必须像$p->yourProperty
一样访问。 描述和价格也在不正确的位置。
还有其他与 SQL 注入相关的问题,这些问题没有包含在本答案中,但至少这将帮助您了解 OOP 本身有什么问题。
最后一个错误是您想从 ProductManager 类的实例 ( $p
) 调用addProduct
并将Product
变量的实例传递给 addProduct 方法:
$p = new ProductManager();
$product = new Product(-1, $POST['name'],$POST['desc'],$POST['price']);
$p->addProduct($c, $product);
我为 $id 参数设置了-1
,因为该行尚未插入。 插入后,您可能希望获取最后一行插入 id 并将其分配给实例属性,例如, $this->id = $lastInsertId;
并且,您需要修复if
语句:
if(isset($_POST['name']) and isset($_POST['desc'])and isset($_POST['price']) ) {
它是$_POST
而不是$POST_
...我们没有看到语句的结果,所以我们没有看到任何与 OO 代码相关的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.