Hi,
I'm fairly new to PHP and am still finding my feet a bit.
I'm creating a site that will hold details about a person, including a photograph. Everything about this person will be stored in a single row in the DB.
I think I've managed to upload the image into the DB OK since there's definitely something in that column, but I can't seem to get it to re-display. Clearly I'm missing something maybe you can point it out to me. I'm not getting any kind of error, just an empty box with an x in it instead of my image.
Ah yes should point out that the image will be stored as a BLOB in the DB.
Here's my code below.
add_info_form.php - The form where the image is uploaded
<form action="add_info_submit.php" name="add_info" method="post" enctype="multipart/form-data">
<table border="0" cellspacing="0" cellpadding="5">
<tr><td>Surname:</td> <td><input name="surname" type="text" size="30"> <i>Required</i></td></tr>
<tr><td>First Name:</td> <td> <input name="firstname" type="text" size="30"> <i>Required</i></td></tr>
<tr><td>Description:</td><td> <textarea name="description" cols="40" rows="10"></textarea></td></tr>
<tr><td>Photo:</td><td> <input name="photo" type="file"> </td></tr>
<tr><td colspan="2"><input type="submit"></td></tr>
</table></form>
add_info_submit.php - where the image is added to the DB
(only the bits relevent to the image)
<?php
....
$filehandle = fopen($_FILES['photo']['tmp_name'], "rb") or die( "Can't open file!" );
$contents = base64_encode(fread($filehandle,$_FILES['act_photo']['size']));
$ephoto = chunk_split($contents);
.....
$result = mysql_query("insert into $database_table (id, surname, firstname, description, photo, hasinfo)
values(null,'$surname','$firstname','$description', '$ephoto', '$hasinfo')",$db)
or die_now("<h2>Could not add act to database table</h2><p>Check database structure</p>");
fclose ($filehandle);
// get the id for the previous insert query. We'll need it
// so we can drag the entry back out
$last_id = mysql_insert_id();
// echo out the most recent addition, for error checking
$result = mysql_query("select id, surname, firstname, description, photo
from $database_table where id = '$last_id'",$db)
or die_now("<h2>Could not echo most recent entry</h2>");
while($row = mysql_fetch_array($result)) {
$the_surname = $row["surname"];
$the_firstname= $row["firstname"];
$the_description= $row["description"];
$the_photo = $row["photo"];
echo("<h3>Latest Entry</h3>
<table border='0' cellpadding='4'>
<tr><td><b>Name:</b></td><td>$the_firstname $the_surname</td></tr>
<tr><td><b>Description:</b></td><td>$the_description</td></tr>
<tr><td><b>Photo:</b></td><td><img src='image.php?id=$last_id'></td></tr>
</table>");
}
?>
image.php - which retrieves and displays the image.
<?
// read in the connection settings
require("include/settings.inc");
// connect to the RDBMS
$db = mysql_connect("$site","$user","$pass")
or die_now("<h2>Could not connect to database server</h2><p>Check passwords and sockets</p>");
// select the database
mysql_select_db("$database",$db)
or die_now("<h2>Could not select database $database</h2><p>Check database name</p>");
$id = $_REQUEST["id"];
$result = @mysql_query("SELECT photo FROM act_database_table WHERE id=" . $id . "");
if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}
while ( $row = mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["photo"];
}
echo base64_decode($encodeddata);
?>