php - 如何修复致命错误:未捕获的 ArgumentCountError?
问题描述
我制作了这个产品类,它在数据库中执行 CRUD 操作我在构造函数方法中出错:
致命错误:未捕获的 ArgumentCountError:函数 Products::__construct() 的参数太少,传递了 0。
我正在尝试从表中保存、获取、更新和删除产品,并将一个图像文件与其他变量一起上传到数据库。我如何上传图像?我的剧本对吗?
我试图跳过构造函数方法以查看其他方法是否独立工作,但不会发生
<?php
error_reporting(E_ALL);
class Products {
public $id;
public $cat_id;
public $name;
public $description;
public $supportedFormats = ['image/png' ,'image/jpeg' ,'image/jpg', 'image/gif'];
public $price;
// Constructor
function __construct($id, $cat_id, $name, $description,$supportedFormats, $price)
{
$this->id = $id;
$this->cat_id = $cat_id;
$this->name = $name;
$this->description = $description;
$this->supportedFormats = $supportedFormats;
$this->price = $price;
}
// Method to show output
function __toString()
{
$output = "<h2> Product id: $this->id</h2> \n " . "<h2> Category id : $this->cat_id </h2> \n" . "<h2> Name : $this->name</h2> \n" . "<h2>Product description: $this->description </h2> \n" ."<h2> Image: $this->supportedFormats". "<h2> Price : $this->price </h2> \n" ;
return $output;
}
// Method for saving product to database
function saveProduct()
{
$con = mysqli_connect("localhost", "root", "root", "my_shop");
$query = "INSERT INTO products VALUES (?,?,?,?,?,?)";
$stmt = $con->prepare($query);
$stmt->bind_param("iissbd", $this->id, $this->cat_id, $this->name, $this->description, $this->supportedFormats, $this->price);
$result = $stmt->execute();
if ($_FILES)
{
$img = $_FILES['filename']['image'];
move_uploaded_file($_FILES['filename']['tmp_name'], $img);
echo "Uploaded image '$img'<br><img src='$img'>";
}
// $img = move_uploaded_file($supportedFormats, 'includes/');
$con->close();
return $result;
}
// Method to update products from database
function updateProduct()
{
$con = mysqli_connect("localhost", "root", "root", "my_shop");
$query = "UPDATE products SET id = ? , cat_id = ?, name = ?, description = ?, supportedFormats = ?, price = ?" . "WHERE id = $this->id";
$stmt = $con->prepare($query);
$stmt->bind_param("iissbd", $this->id, $this->cat_id, $this->name, $this->description, $this->supportedFormats, $this->price);
$result = $stmt->execute();
if ($_FILES)
{
$img = $_FILES['filename']['image'];
move_uploaded_file($_FILES['filename']['tmp_name'], $img);
echo "Uploaded image '$img'<br><img src='$img'>";
}
// $img = move_uploaded_file($supportedFormats, 'includes/');
$con->close();
return $result;
}
// Method to remove product from table
function removeProduct()
{
$con = mysqli_connect("localhost", "root", "root", "my_shop");
$query = "DELETE FROM products WHERE id = $this->id";
$result = $con->query($query);
$con->close();
return $result;
}
// Method to get all products
static function getProduct()
{
$con = mysqli_connect("localhost", "root", "root", "my_shop");
$query = "SELECT * FROM products";
$result = $con->query($query);
if (mysqli_num_assoc($result) > 0)
{
$products = array();
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$product = new Product($row['id'],$row['cat_id'],$row['name'],$row['description'],$row['supportedFormats'],$row['price']);
array_push($products, $product);
unset($product);
}
$con->close();
return $result;
}
else
{
$con->close();
return NULL;
}
}
// Method to find bidders
static function findProducts()
{
$con = mysqli_connect("localhost", "root", "root", "my_shop");
$query = "SELECT * FROM products WHERE id = $id";
$result = $con->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC);
if ($row)
{
$product = new Product($row['id'],$row['cat_id'],$row['name'],$row['description'],$row['supportedFormats'],$row['price']);
$con->close();
return $result;
}
else
{
$con->close();
return NULL;
}
}
}
// $prod1 = new Products();
// echo findProducts();
?>
我希望它在数据库中执行 crud 功能
解决方案
该类不必包含构造函数。但是,当类具有构造函数时,在创建对象时,您必须指定将发送给构造函数的参数。
推荐阅读
- r - 从R中的csv绘制分组条形图
- javascript - Javascript - 在范围之间分配相应的值
- ruby-on-rails - Rails 6.1 如何将文件渲染到页面模板中
- ios - UIPasteBoard 在 ios 中一次又一次地实例化只发生在 M1 Air
- python - 如何检查我的查询是否成功完成
- ansible - 无法从 ansible 任务输出读取结果字典中的 stderr
- java - 当有奇数队号时,我的夹具算法抛出异常
- python - 如何在 python 中使用/查找 Latex 编译器
- excel - Excel - 对于多列数据,SumProduct 中是否有比 SumIf 更好的解决方案
- azure - Azure 逻辑应用,解析的 JSON 在使用时返回 Null