Advertisement

12.29.2007 at 10:25PM PST, ID: 23049494
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

combining a $_FILES upload method field with other fields (not directly related) when submitting a form.

Asked by naubrey in PHP and Databases, PHP Scripting Language, WebApplications

Tags: , ,

I am building a markbook web solution using PHP, MySQL etc to store student names, marks and picture.

I am using an input field of type "file" to upload a picture and store the file path to each picture in a MySQL database. The uploaded picture becomes visible when uploaded and all is working OK except When I return to edit the value in other fields in my form that I am using e.g. testmark1 I loose the file path because the value of $_FILES['file']['name'] is now ZERO. It therefore deletes the complete file path that was working before.

 I am using the <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> to POST to itself.
My question is "Should I be submitting to an external  PHP file" or is it possible to work around this"?
My general question in summary is "How to combine an (upload and display picture code) inside a form with other input fields"?
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
<? 
// edit.php - student data
?>
<? 
// includes connection script and the file path to upload my pictures
include("../config.php"); 
// form not yet submitted 
// display initial form with values pre-filled 
if (!isset($_POST['submit'])) 
{ 
// open database connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
 
// select database 
mysql_select_db($db) or die ("Unable to select database!"); 
 
// generate and execute query 
$id = mysql_escape_string($_GET['id']); 
$query = "SELECT studentid, first, surname, year, theclass, asses1, asses2, asses3, asses4, asses5, total, outof, percentage, picture, comment_one, comment_two FROM students WHERE studentid = '$id'"; 
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); 
//neil error check remove when completed
echo $query;
 
// if a result is returned 
if (mysql_num_rows($result) > 0) 
{ 
// turn it into an object 
$row = mysql_fetch_object($result); 
 
// print form with values pre-filled 
 
?>
 
<table width="500" cellpadding="2" cellspacing="2">
  <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="studentid" value="<? echo $id; ?>">
    <tr>
      <td valign="top"><b><font size="-1">student id</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="studentid" value="<? echo $row->studentid; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">surname</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="surname" value="<? echo $row->surname; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">first</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="first" value="<? echo $row->first; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">year</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="year" value="<? echo $row->year; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">theclass</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="theclass" value="<? echo $row->theclass; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">asses1</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="asses1" value="<? echo $row->asses1; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">asses2</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="asses2" value="<? echo $row->asses2; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">asses3</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="asses3" value="<? echo $row->asses3; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">asses4</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="asses4" value="<? echo $row->asses4; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">asses5</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="asses5" value="<? echo $row->asses5; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">total</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="total" value="<? echo $row->total; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">outof</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="outof" value="<? echo $row->outof; ?>"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">Percentage</font></b></td>
      <td><input size="50" maxlength="250" type="text" name="percentage" value="<? echo $row->percentage; ?>"></td>
    </tr>
    <tr><!-- HERE IS THE DISPLAY FILE PATH TO PICTURE FIELDS-->
      <td valign="top"><b><font size="-1">Picture file path</font></b></td>
      <td><input size = "100" type="text" maxlength="300" name="picture" value="<? echo $row->picture; ?>"></td>
    </tr>
    <tr><!-- HERE IS THE DISPLAY PICTURE TAG>
      <td><img src="<? echo $filepath.($row->picture); ?>"/></td>
    </tr>
    <tr><!-- HERE IS FILE UPLOAD FIELD-->
      <td><input type="file" name="file" size="45"></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">Comment one</font></b></td>
      <td><textarea name="comment_one" cols="40" rows="10"><? echo $row->comment_one; ?></textarea></td>
    </tr>
    <tr>
      <td valign="top"><b><font size="-1">Comment two</font></b></td>
      <td><textarea name="comment_two" cols="40" rows="10"><? echo $row->comment_two; ?></textarea></td>
    </tr>
    <tr>
      <td colspan=2><input type="Submit" name="submit" value="Update"></td>
    </tr>
    <tr></tr>
  </form>
</table>
<? 
} 
// no result returned 
// print graceful error message 
else 
{ 
echo "<font size=-1>That student could not be located in our database.</font>"; 
} 
} 
else 
{ 
// form submitted 
// start processing it 
} 
?>
<!-- page footer - snip -->
<? 
// edit.php - edit this student list talking to itself
?>
<!-- page header - snip -->
<? 
// form not yet submitted 
// display initial form with values pre-filled 
if (!isset($_POST['submit'])) 
{ 
// form display code 
} 
else 
{ 
// form submitted 
// start processing it 
 
// set up error list array 
$errorList = array(); 
$count = 0; 
 
// validate text input fields 
$first = mysql_escape_string($_POST['first']); 
$surname = mysql_escape_string($_POST['surname']); 
$year = mysql_escape_string($_POST['year']); 
$studentid = mysql_escape_string($_POST['studentid']); 
$theclass = mysql_escape_string($_POST['theclass']); 
$asses1 = mysql_escape_string($_POST['asses1']); 
$asses2 = mysql_escape_string($_POST['asses2']); 
$asses3 = mysql_escape_string($_POST['asses3']); 
$asses4 = mysql_escape_string($_POST['asses4']); 
$asses5 = mysql_escape_string($_POST['asses5']); 
$total = $asses1 + $asses2 + $asses3 + $asses4 + $asses5; 
//$total = mysql_escape_string($_POST['total']); 
$outof = mysql_escape_string($_POST['outof']); 
$percentage = mysql_escape_string($_POST['percentage']);
$picture = mysql_escape_string($_POST['picture']); 
$comment_one = mysql_escape_string($_POST['comment_one']);
$comment_two = mysql_escape_string($_POST['comment_two']);
$file = $filepath.mysql_escape_string($_FILES['file']['name']);
// error checking code below that I am not using
if (!$contact) { $contact = $def_contact; } 
 
// check for errors 
 
echo $errorlist;
 
if (sizeof($errorList) == 0) 
{ 
// open database connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
 
// select database 
mysql_select_db($db) or die ("Unable to select database!"); 
 
// generate and execute query // test removed picture = '$file' replaced with  picture=$picture
$query = "UPDATE students SET first = '$first', surname = '$surname', year = '$year',  studentid='$studentid', theclass ='$theclass', asses1 = '$asses1', asses2 = '$asses2', asses3 = '$asses3', asses4 = '$asses4', asses5 = '$asses5', total = '$total', outof = '$outof', percentage ='$percentage', picture = '$file', comment_one = '$comment_one', comment_two = '$comment_two' WHERE studentid = '$studentid' "; 
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); 
 
// print result 
echo "<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>"; 
//neil error check
echo $query;
// close database connection 
mysql_close($connection); 
} 
else 
{ 
// errors occurred 
// print as list 
echo "<font size=-1>The following errors were encountered: <br>"; 
echo "<ul>"; 
for ($x=0; $x<sizeof($errorList); $x++) 
{ 
echo "<li>$errorList[$x]"; 
} 
echo "</ul></font>"; 
}
}
?>
<?php 	// my upload handler here
 
	if( $_FILES['file']['name'] != "" )
	{
	  copy ( $_FILES['file']['tmp_name'], //change this path when uploaded to server
		 "C:/xampp/htdocs/student_markbook/admin/temp/" . $_FILES['file']['name'] ) 
	  or die( "Could not copy file" );
	}
	else
	{
	  die( "No file specified" );
	}
 
?>
[+][-]12.30.2007 at 04:25AM PST, ID: 20550559

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.30.2007 at 05:33PM PST, ID: 20552965

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.31.2007 at 07:44PM PST, ID: 20558650

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.31.2007 at 09:44PM PST, ID: 20558814

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP and Databases, PHP Scripting Language, WebApplications
Tags: php, firefox and IE, internal using xamp, mySQL etc
Sign Up Now!
Solution Provided By: schmavey
Participating Experts: 2
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628