duanchongzi9997 2019-05-04 12:33 采纳率: 0%
浏览 2203

如何修复致命错误:Uncaught ArgumentCountError?

我创建了这个 product 类,用于在数据库中执行 CRUD 操作,在构造函数方法中出错了:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Products::__construct(), 0 passed.

我试图保存、获取、更新和删除表中的产品,并将一个图像文件与其他变量一起上传到数据库。如何上传图片?我的脚本对吗?

我试图跳过构造函数方法,以查看其他方法是否独立工作,但它不奏效。

<?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> 
 " . "<h2> Category id : $this->cat_id </h2> 
" . "<h2> Name : $this->name</h2> 
" . "<h2>Product description: $this->description </h2> 
" ."<h2> Image: $this->supportedFormats". "<h2> Price : $this->price </h2> 
"  ;
        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功能。

  • 写回答

1条回答 默认 最新

  • dongshou9343 2019-05-04 15:27
    关注

    The class does not have to contain a constructor. However, when the class has a constructor, when creating objects, you must specify the arguments that will be sent to the constructor.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘