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

如何修复致命错误: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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化