View Full Version : help with uploading image to folder from database id

02-23-2010, 11:13 PM
Hie people i'm trying to create an update link that will upload an image onto to the server from the with the id number as a refrence when that information is viewed again, but all the time when i upload the image it does nothing, used the echo to view what id number was output and at the moment the $_post has the database id where the image is linked to

// Connect to database
include_once "../../scripts/connect_to_members_mysql.php";

//check for required from variables
if (!$_POST[id]){
header("Location: /pick_modrecord.php");

// Parsing section for the member picture... only runs if they attempt to upload or replace a picture
if ($_POST['parse_var'] == "pic"){

if (!$_FILES['fileField']['tmp_name']) {

$error_msg = '<font color="#FF0000">ERROR: Please browse for an image before you press submit.</font>';

} else {

$maxfilesize = 51200; // 51200 bytes equals 50kb
if($_FILES['fileField']['size'] > $maxfilesize ) {

$error_msg = '<font color="#FF0000">ERROR: Your image was too large, please try again.</font>';

} else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['fileField']['name'] ) ) {

$error_msg = '<font color="#FF0000">ERROR: Your image was not one of the accepted formats, please try again.</font>';

} else {

$newname = "image01.jpg";
$place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$_POST[id]/".$newname);
$success_msg = '<font color="#009900">Your image has been updated, it may take a few minutes for the changes to show... please be patient.</font>';

} // close else that checks file exists

//build and issue query
$sql = "SELECT id, firstname, lastname, burial_site, date_of_burial, bio_body FROM $table_name WHERE id = '$_POST[id]'";
$result = @mysql_query($sql) or die(mysql_error());

//get results for display
While ($row = mysql_fetch_array($result)){
$firstname = $row['firstname'];
$lastname= $row['lastname'];
$burial_site = $row['burial_site'];
$date_of_burial = $row['date_of_burial'];
$bio_body = $row['bio_body'];
/////// Mechanism to Display Pic. See if they have uploaded a pic or not //////////////////////////
$check_pic = "../../members/$_POST[id]/image01.jpg";
$default_pic = "../../members/0/image01.jpg";
if (file_exists($check_pic)) {
$user_pic = "<img src=\"$check_pic\" width=\"100px\" />"; // forces picture to be 100px wide and no more
} else {
$user_pic = "<img src=\"$default_pic\" width=\"100px\" />"; // forces default picture to be 100px wide and no more
echo "$id";
echo "$_POST[id]";
echo "$_POST[id]";

this is the front end as well

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Record Management System(RMS) - Modify Record</title>
<link href="style/main.css" rel="stylesheet" type="text/css" />
<link rel="icon" href="http://www.yourdomain.com/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="http://www.yourdomain.com/favicon.ico" type="image/x-icon" />
<link href="../../style/main.css" rel="stylesheet" type="text/css" />

<h1>Record management system</h1>
<h2><em> Modify a Record</em></h2>

<FORM METHOD="POST" ACTION ="do_modrecord.php">
<INPUT TYPE="hidden" name="id" value="<? echo "$_POST[id]"; ?>">

<table cellspacing=3 cellpadding=5>
<tr> <th width="210">
Name &amp; Site Burial Information</th>
<th colspan="2">OTHER INFORMATION</th>
<td valign=top><?php print "$user_pic"; ?></td>
<td width="436" valign=top><input name="fileField" type="file" class="formFields" id="fileField" size="42" />
50 kb max</TD>
<td valign=top>
<p><STRONG> First Name: </STRONG><BR>
<INPUT TYPE="text" NAME = "firstname" VALUE ="<? echo "$firstname"; ?>" SIZE=35 MAXLENGTH=100></p>
<p><STRONG> Last Name: </STRONG><BR>
<INPUT TYPE="text" NAME = "lastname" VALUE ="<? echo "$lastname"; ?>" SIZE=35 MAXLENGTH=100></p>
<p><STRONG> Burial Site</STRONG><BR>
<INPUT TYPE="text" NAME = "burial_site" VALUE ="<? echo "$burial_site"; ?>" SIZE=35 MAXLENGTH=100></p></td>
<td colspan="2" valign=top>
<p><STRONG> Date of Burial (YYYY/MM/DD)</STRONG><BR>
<INPUT TYPE="text" NAME = "date_of_burial" VALUE ="<? echo "$date_of_burial"; ?>" SIZE=35 MAXLENGTH=100></p>
<p><STRONG> Bio Body</STRONG><BR>
<textarea name="bio_body" cols="100" rows="10"><? echo "$bio_body"; ?></textarea>

<td align=center colspan=3><br>
<input name="parse_var" type="hidden" value="pic" />
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="Update Record">
<p><a href="../members.php">Return to Admin page</a></p></TD>


02-23-2010, 11:23 PM
Just looking at the html, you cannot upload with a standard form.
You'll need to add enctype="multipart/form-data" to you're form tag in order to deal with uploads, and of course it has to be post method (which you do have).

I'm a little busy atm so I can't look over the rest of this, but post back if you still have problems and I'll see if I can help tonight before I go home (I don't have the net at home right now)

02-23-2010, 11:27 PM
sorry don't mean to be blunt, but where abouts u want me to put that code :confused:, thanks :thumbsup:

02-23-2010, 11:39 PM
Sorry, replace the form tag in the html itself:

<!-- replace this: -->
<FORM METHOD="POST" ACTION ="do_modrecord.php">

<!-- with this -->
<FORM METHOD="POST" ACTION ="do_modrecord.php" enctype="multipart/form-data">

And sorry I won't be able to help past this, so I hope this is the only trouble O.o
this is a common pitfall with file uploads in php.

02-24-2010, 12:07 AM
thanks for that, getting some were now, its now saying can't find the folder its moving it to, hopefully i can sort it out... if not be back :thumbsup:

02-24-2010, 12:14 AM
just found the problem in my do_modrecord.php because it working in different folders had to add ../../, thanks for the help :thumbsup:

02-24-2010, 03:37 PM
Great, glad you got it working mate :thumbsup: