I’m currently having a bit of a rough time uploading an image to my database. I currently have multiple variables/inputs being uploaded from one form – one if these inputs is an image file upload. The file seems to make it do the database, but when I try to retrieve the image via a PHP script, it simply returns “Array”, rather than the image. Any help? Thanks!
Here’s the upload code:
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$projectname = htmlentities($_POST['projectname'], ENT_QUOTES);
$item = htmlentities($_POST['item'], ENT_QUOTES);
$description = htmlentities($_POST['description'], ENT_QUOTES);
$neededby = htmlentities($_POST['neededby'], ENT_QUOTES);
$shipping= htmlentities($_POST['shipping'], ENT_QUOTES);
$revisions = htmlentities($_POST['revisions'], ENT_QUOTES);
$price = htmlentities($_POST['price'], ENT_QUOTES);
$paid = htmlentities($_POST['paid'], ENT_QUOTES);
$ordered1 = htmlentities($_POST['ordered1'], ENT_QUOTES);
$ordered2 = htmlentities($_POST['ordered2'], ENT_QUOTES);
$ordered3 = htmlentities($_POST['ordered3'], ENT_QUOTES);
$received1 = htmlentities($_POST['received1'], ENT_QUOTES);
$received2 = htmlentities($_POST['received2'], ENT_QUOTES);
$received3 = htmlentities($_POST['received3'], ENT_QUOTES);
$shipped1 = htmlentities($_POST['shipped1'], ENT_QUOTES);
$shipped2 = htmlentities($_POST['shipped2'], ENT_QUOTES);
$shipped3 = htmlentities($_POST['shipped3'], ENT_QUOTES);
$tracking = htmlentities($_POST['tracking'], ENT_QUOTES);
$delivered = htmlentities($_POST['delivered'], ENT_QUOTES);
$thestatus = htmlentities($_POST['thestatus'], ENT_QUOTES);
$photo=($_FILES['photo']);
if ($projectname == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in project name!';
renderForm($projectname, $item, $description, $neededby, $shipping, $revisions, $price, $paid, $ordered1, $ordered2, $ordered3, $received1, $received2, $received3, $shipped1, $shipped2, $shipped3, $tracking, $delivered, $thestatus, $photo, $error, $id);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT todo (projectname, item, description, neededby, shipping, revisions, price, paid, ordered1, ordered2, ordered3, received1, received2, received3, shipped1, shipped2, shipped3, tracking, delivered, photo, thestatus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
{
$stmt->bind_param("sssssssssssssssssssss", $projectname, $item, $description, $neededby, $shipping, $revisions, $price, $paid, $ordered1, $ordered2, $ordered3, $received1, $received2, $received3, $shipped1, $shipped2, $shipped3, $tracking, $delivered, $photo, $thestatus);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: main.php");
}
}
And the file retrieve code:
<?php
mysql_connect("localhost","MYUSER","MYPASS");
mysql_select_db("MYDB");
$query = "SELECT photo FROM todo where id=$id";
$result = MYSQL_QUERY($query);
$data = MYSQL_RESULT($result,0,"photo");
Header( "Content-type: $type");
print $data;
?>
The mysql column is a BLOB type.
Here is an image so you can get some visuals on what I’m saying:
https://i.stack.imgur.com/4pHwB.png
Try this tutorial
http://www.tizag.com/phpT/fileupload.php
put the file name on variable then insert it into the database (I’m expecting you know how to retrieve data from databases)