Here is an example table:
CREATE TABLE business_dir (
bus_id tinyint(4) NOT NULL, auto_increment,
bus_BusName varchar(64) default NULL,
bus_ImageURL1 varchar(64) default NULL,
bus_ImageURL2 varchar(64) default NULL,
PRIMARY KEY (bus_id)
) TYPE=MyISAM;
I have it so my admin can add and upload the images. I'm just using the copy() function and keeping track of the Image_name.jpg within the bus_ImageURL field name. This works great, and I've been using it for some time.
Although, I need to allow the enduser the ability to go and change just the bus_ImageURLx field.
In my EDIT Section, I created a link to a secondary file called imgupdate.php. I pass the bus_id and the $field name. Here is my code:
URL: imgupdate.php?id=$row[bus_id]&field=$row[bus_ImageURLx] (I know its passing the variable fine, I've echoed them out for debugging).
/* SHOW THE FORM TO UPDATE THE IMAGE */
echo "
<form action=\"$PHP_SELF\" method=\"post\" enctype=multipart/form-data>
<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"200000\">
<input type=\"hidden\" name=\"id\" value=\"$id\">
<input type=\"hidden\" name=\"action\" value=\"Update\">
<input type=\"hidden\" name=\"field\" value=\"$field\">
<input type=\"file\" name=\"$field\" size=\"30\"><br>
<input type=\"SUBMIT\" value=\"Update\">
</form>";
if($action=="Update") {
/* EXECUTE THE UPDATES */
/* COPY THE FILE */
$fieldname = "$field"."_name";
$basedir = "$CFG->busdir_files/$id";
if ($fieldname != "") {
copy($field, "$basedir/$fieldname");
echo "<p align=\"center\" class=\"red_bold\">Your Image has been Successfully Updated!</p>";
} else {
echo "<p>ERROR: The file for $field did not upload properly :: $fieldname</p>";
}
/* NOW RUN THE QUERY UPDATE */
$upquery = ("UPDATE $table SET $field='$fieldname' WHERE bus_id=$id ");
$upresult = mysql_query($upquery, $connection) or die ("Error in query $table Update Record");
}