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

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

我正在尝试建立一个可以使用 php 和 mysql 上传多张图片的网站,但出现以下错误:

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]);

您似乎在使用 PHP 8.0。这个版本不允许在 execute() 方法中传递数组;它仅从 PHP 8.1 开始可用。您必须使用 bind_param() 方法绑定参数。

$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();

您还应该考虑使用 PDO 而不是 mysqli,这会让您的生活更轻松。使用 PDO,您始终可以将数组传递给 execute().