Php 无效参数编号:混合命名参数和位置参数

Php 无效参数编号:混合命名参数和位置参数,php,mysql,pdo,Php,Mysql,Pdo,这里我想要的目标是允许使用PDO通过PHP将图像上传到数据库 数据库不会有太多的值,最多10到15个,所以我觉得允许图像上传到那里会更舒服 以下是我的PHP代码: <?php require '../../app/start.php'; if (!empty($_POST)) { $name = $_POST['name']; $position = $_POST['position']; $detail = $_POST['detail']; $imageDat

这里我想要的目标是允许使用PDO通过PHP将图像上传到数据库

数据库不会有太多的值,最多10到15个,所以我觉得允许图像上传到那里会更舒服

以下是我的PHP代码:

<?php 
require '../../app/start.php';
if (!empty($_POST)) {
$name       = $_POST['name'];
$position   = $_POST['position'];
$detail     = $_POST['detail'];
$imageData = addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$insertPage = $db->prepare("
INSERT INTO about (name, position, detail, imageType, imageData)
VALUES (:name, :position, :detail, {$imageProperties['mime']}, {$imageData})
");
$insertPage->execute([
'name'      => $name,
'position'  => $position,
'detail'    => $detail,
'imageType' => $imageProperties['mime'],
'imageData' => $imageData
]);
}
header('Location: ' . BASE_URL . '/admin/about/list.php');
require VIEW_ROOT . '/admin/about/add.php'; 
?>
我不确定上面是否定义了该表。老实说,我对你所指的并不熟悉

以下是我在尝试上载图像时遇到的两个错误:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters in /admin/about/add.php on line 24    
Warning: Cannot modify header information - headers already sent by (output started at /admin/about/add.php:24) in /admin/about/add.php on line 26
使用这个,我得到了几个警告,图像没有上传

如何修复代码以允许将图像上载到数据库

编辑:很抱歉,我以前复制并粘贴了错误的html代码。

$insertPage = $db->prepare("
INSERT INTO about (name, position, detail, imageType, imageData)
VALUES (:name, :position, :detail, {$imageProperties['mime']}, {$imageData})
");
$insertPage->execute([
'name'      => $name,
'position'  => $position,
'detail'    => $detail,
'imageType' => $imageProperties['mime'],
'imageData' => $imageData
]);
应该是

$insertPage = $db->prepare("
INSERT INTO about (name, position, detail, imageType, imageData)
VALUES (:name, :position, :detail, :imageType, :imageData)
");
$insertPage->execute([
'name'      => $name,
'position'  => $position,
'detail'    => $detail,
'imageType' => $imageProperties['mime'],
'imageData' => $imageData
]);

请注意INSERT查询的VALUES部分中的更改。这将修复您得到的错误。我没有深入探讨您试图通过此查询实现的目标,我认为最好将图像存储在文件系统中,将URL存储在DB中。表是如何定义的?如果您希望数据库保持较小,为什么要将图像上载到它?这将产生相反的效果。这种逻辑使我无法理解。很少有情况下,将图像上传到数据库是个好主意。图像是文件,所以只需将其保存在文件系统中。数据库的目的是保持较小的规模,因为它不是一家大公司。在这种情况下,您不将图像上载到数据库,而是将其存储在文件夹中,然后将其路径/url保存到数据库,将图像存储在文件位置,并将该位置的路径放入database@MagnusEriksson将图像存储到数据库而不是文件系统是有原因的——一个原因是负载平衡设置利用了多个php节点,可能还有一个MySQL集群,因此您不知道哪个节点可能最终存储文件。当然,可以使用某种网络挂载来纠正这个问题,但只需将内容粘贴到db即可。将图像存储到数据库中没有问题。在我们的IT世界里,我们不能像你刚才那样处理绝对的问题。原因是-您可能是非常错误的,就像以前一样。使用您的更改,我会得到一个稍微不同的错误代码:SQLSTATE[HY093]:无效参数编号:未定义参数您确定在此处发布了正确的查询吗?此错误意味着您在查询中定义的参数数量与数组中的项目数量不同。您一定遗漏了什么。这次我复制并粘贴了你的更改,效果很好。非常感谢。不客气。那么请把答案标为已解决
$insertPage = $db->prepare("
INSERT INTO about (name, position, detail, imageType, imageData)
VALUES (:name, :position, :detail, {$imageProperties['mime']}, {$imageData})
");
$insertPage->execute([
'name'      => $name,
'position'  => $position,
'detail'    => $detail,
'imageType' => $imageProperties['mime'],
'imageData' => $imageData
]);
$insertPage = $db->prepare("
INSERT INTO about (name, position, detail, imageType, imageData)
VALUES (:name, :position, :detail, :imageType, :imageData)
");
$insertPage->execute([
'name'      => $name,
'position'  => $position,
'detail'    => $detail,
'imageType' => $imageProperties['mime'],
'imageData' => $imageData
]);