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

时间:2017-02-13 15:54:43

标签: php mysql pdo

我期望的目标是允许使用PDO通过PHP将图像上传到数据库。

数据库不会有很多值,最多十到十五,所以我觉得让图像上传到那里感觉更舒服。

这是我的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'; 
?>

这是我的HTML:

<form action="<?php echo BASE_URL; ?>/admin/about/add.php" method="POST" enctype="multipart/form-data" autocomplete="off">
        <label for="name">
            Name
            <input type="text" name="name" id="name" value="">
        </label>

        <label for="position">
            Position
            <input type="text" name="position" id="position" value="">
        </label>

        <label for="detail">
            Detail
            <textarea name="detail" id="detail" cols="30" rows="10"></textarea>
        </label>

        <label for="imageId">
            Upload Image File:
            <input name="userImage" id="userImage" type="file" class="inputFile" />
        </label>

        <input type="submit" value="Add Page">
    </form>

表:

id  int(11) NO  PRI     auto_increment  
imageId tinyint(3)  NO              
imageType   varchar(25) YES             
imageData   mediumblob  NO              
name    varchar(35) NO              
position    varchar(35) NO              
detail  varchar(120)    NO              

我不确定上面是否定义了表格。老实说,我不熟悉你指的是什么。

这是我在尝试上传图片时遇到的两个错误:

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代码。

1 个答案:

答案 0 :(得分:2)

$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存储在数据库中

相关问题