'Uncaught ArgumentCountError: mysqli_stmt::execute() expects exactly 0 arguments, 1 given

I am trying to build a site where I can upload multiple images with php and mysql but I am getting below error:

Fatal error: Uncaught ArgumentCountError: mysqli_stmt::execute() expects exactly 0 arguments, 1 given in C:\xampp\htdocs\RENT\component\test-folder\upload.php:37 Stack trace: #0 C:\xampp\htdocs\RENT\component\test-folder\upload.php(37): mysqli_stmt->execute(Array) #1 {main} thrown in C:\xampp\htdocs\RENT\component\test-folder\upload.php on line 37

#inserting image name into database
$sql = "INSERT INTO image_upload (image_name) VALUES (?)";
$stmt = $conn->prepare($sql);
$stmt->execute([$new_img_name]);


Solution 1:[1]

It looks like you are using PHP 8.0. This version didn't allow passing an array in the execute() method; it's only available as of PHP 8.1. You must bind the parameters using the bind_param() method.

$sql = "INSERT INTO image_upload (image_name) VALUES (?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $new_img_name); // one s for each parameter
$stmt->execute();

You should also consider using PDO instead of mysqli which would make your life easier. With PDO you could always pass an array to execute().

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1