如何修复致命错误:未捕获的ArgumentCountError?

时间:2019-05-04 12:33:05

标签: php mysql

我做了这个产品类,它在数据库中执行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功能

1 个答案:

答案 0 :(得分:0)

该类不必包含构造函数。但是,当类具有构造函数时,在创建对象时,必须指定将发送给构造函数的参数。