Code: Select all
// move_uploaded_file($_FILESї'userfile']ї'tmp_name'], "../../images/Contests/");-----
I don't know your table structure or how a record set is initially created in your system. So the only thing I can do is to give you an example.
Let's assume a (simple) table 'contests' having
Code: Select all
`id` tinyint(3) unsigned NOT NULL auto_increment,
`startdate` date default NULL,
<-- much more plain data here -->
`graphics` blob,
UNIQUE KEY `id` (`id`),
KEY `id_2` (`id`)Code: Select all
$query = 'SELECT id,startdate,... from contests';
$result = mysql_query($query, $conection) or die(mysql_error());
while ($row = mysql_fetch_array($result))
prettyOutputRow($row);The common way is to add a src-attribute containing the source-url for the image data (<img src="image1.png"/>). So you will add a url pointing to a php-script that will pull the appropriate image-data from the db when requested. Appropriate means the image related to a certain 'contests'-entry identified by its unique identifier (in this example the field 'id').
In prettyOutputRow($row) you have to add something like
Code: Select all
echo '<img src="contestImg.php?contestid=', $rowї'id'], '" />'Code: Select all
...contest starts at 2002/09/30<br/>
<img src="contestImg.php?imgid=2"/>...All you have to do in this script is to fetch the argument ($_GET['contestid']) and query the related image data
Code: Select all
<?php
if ($_GETї'contest'])
{
/* <-- data base connection code here --> */
// as mentioned the image-type-handling I leave up to you
// assuming png for now
header('Content-type: image/png');
// only querying the field <graphics>, nothing else needed
// and only the record of the requested contest
$query ='SELECT graphics from contests WHERE id='.(int)$_GETї'contest'];
// any error logging to the log-file, since Content-type: image/png already set
$result = mysql_query($query, $conn) or die(error_log('imgData query failed'));
if ( ($row = mysql_fetch_row($result)) !== FALSE)
{ // if a feasable record-set was found, return the graphics-data (if there's any)
if (!is_null($rowї0]))
echo $rowї0];
else
defaultNoPicture(); // no picture provided (yet)
}
else
// if no such record was found (malformed request?) display an error-image (optional)
echoErrorImage();
}
else
echoErrorImage();
?>to do this you have to provide not only the image-data but also the contest-id (somewhere the data has to be replaced and the script has to know)
(contestimgupload.php):
Code: Select all
if (isset($_POSTї'contestid']))
{
/* <-- data base connection code here --> */
/* <-- ingenious code to determine wether the user is allowed to change this data or not --> */
if (isset($_FILESї'graphic']) && is_uploaded_file ($_FILESї'graphic']ї'tmp_name']))
{
$data = '';
/* reading the (binary) uploaded data */
$fd = fopen($_FILESї'graphic']ї'tmp_name'], 'rb');
while($nextpart = fread($fd, 4096)) // or some other
$data .= $nextpart; // tricky read-method
fclose($fd);
unlink($_FILESї'graphic']ї'tmp_name']);
/* request replacement of blob data */
$queryG = "UPDATE contests set graphics='". mysql_escape_string($data) ."' WHERE id=".$_POSTї'contestid'];
mysql_query($queryG, $connection) or die('volka is dumb! '.mysql_error());
/* if ( mysql_affected_rows($connection) == 0)
< the code at the top of the page wasn't that ingenious >
*/
echo 'image data for contest #',$_POSTї'contestid'],' has been updated';
}
else
print ("no image data provided.");
}The script to create the form is very similar to the 'overview'-script. Maybe a little more complex select-statement to show only contest-records the user is allowed to change (nevertheless you have to check this in the script that performs the upload).
in the function prettyOutputRow you now will print something like
Code: Select all
<?php ...
function prettyOutputRow($row) {
echo '<fieldset><legend>contest #', $rowї0],'</legend>'; // id was the first field selected
...
?>
<form enctype="multipart/form-data" method="post" action="contestimgupload.php">
<input type="hidden" name="contestid" value="<?php echo $rowї0]; ?>"/> <br/>
<input type="file" name="graphic" /> <br/>
<input type="submit" />
</form>
...
</fieldset><?php
}some parts are still open (i.e. deleting the image data) but I already have to explain to somebody why I'm still sitting at my PC (sunday, 8am)