Posted: Sun Sep 29, 2002 12:58 am
I just noticed the line in your script. If you have decided not to store the binary data in the database stop reading and say so 
-----
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' havingYou probably have a script where you output the list of contests. Something liketo add the picture to this output you have to add an <img>-element to the document. Unfortunately you cannot add the image data directly to the same page (there is a way to add base64-encoded image data inline, but afaik it's netscape-only).
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 likeso the client browser will 'see' something likeit will then request the image data for the <img>-element. whatever data is returned 'from' this url (contestImg.php) should be only the imagedata preceded by a "content-type- (and optionally but good behaviour a"content-length"-) header.
All you have to do in this script is to fetch the argument ($_GET['contestid']) and query the related image datanow back to the INSERT-problem. My code-snippet is not useful for this context. You may have already changed it to your needs and included it to the query that creates the 'contests'-entry, maybe you want the system to allow a change of the image data by authorized users.
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):
add a hidden field to your form that will transport the contestid from the script that creates the form to the upload-script.
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(all scripts untested)
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)
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)