My desired goal here is to allow the uploading of images to the database through PHP, using PDO.
The database is not going to have a lot of values, at most ten to fifteen so I feel more comfortable with allowing the images to be uploaded there.
Here is my PHP code:
<?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';
?>
and here is my 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>
Table:
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
I'm unsure if the above defines the table. I honestly am not familiar with what you are referring to.
These are the two errors I'm getting when I'm trying to upload the image:
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
Using this, I get a couple warnings and the image does not upload.
How can I fix my code to allow the uploading of images to the database?
Edit: I apologize, I previously copy and pasted the wrong html code.