Uploading image to server: INSERT works, UPDATE doesn't
Posted: Thu May 19, 2005 1:16 pm
I'm working on a very small database for a client so that she can maintain her own Staff page online.
I have written code to upload an image along with other information when there is a new employee, but I am having trouble with uploading a new image when editing an employee.
To add an employee, this works:
To update an employee I have been trying several variations on this code, but the image filename is not being read when uploading:
Can anyone help me figure out where I've gone wrong?
I have written code to upload an image along with other information when there is a new employee, but I am having trouble with uploading a new image when editing an employee.
To add an employee, this works:
Code: Select all
<?php
if($HTTP_POST_VARS['Upload'] == "Upload"){
}
if($_POST['Upload'] == "Upload"){
$originalImageFileName = $_FILES['StaffPhoto']['name'];
$tempImageFileName = $_FILES['StaffPhoto']['tmp_name'];
$siteRoot = $_SERVER["DOCUMENT_ROOT"];
$uploadDir = $siteRoot . "/staffadmin/";
$newImagePath = "/staffadmin/photos/" . $originalImageFileName;
umask(0);
move_uploaded_file($tempImageFileName, $newImagePath);
system("chmod 755 $newPath");
$query = "INSERT INTO staff (
Category, List_no, StaffName, StaffTitle, Extension, StaffPhoto)
VALUES (' " . $_POST['Category'] . " ', ' " . $_POST['List_no'] . " ', ' " . $_POST['StaffName'] . " ', ' " . $_POST['StaffTitle'] . " ', ' " . $_POST['Extension'] . " ', ' " . $_FILES['StaffPhoto']['name'] . " ')";
$result = mysql_query($query);
$lastInserted = mysql_insert_id();
}
?><form action="<?php echo $_SERVER['SCRIPT_URI']; ?>" method="post" enctype="multipart/form-data" name="Upload" id="Upload">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><strong>Category:</strong></td>
<td><select name="Category" class="bodytext">
<option selected>Select Category...</option>
<option value="1">Owner</option>
<option value="2">Office</option>
<option value="3">New Vehicles</option>
<option value="4">Used Vehicles</option>
<option value="5">Collision Center</option>
<option value="6">Service Department</option>
<option value="7">Parts Counter</option>
<option value="8">Building Maintenance</option>
</select></td>
</tr>
<tr>
<td><strong>Order Within Category:</strong></td>
<td><input name="List_no" type="text" id="List_no" size="10" maxlength="3" class="bodytext"></td>
</tr>
<tr>
<td><strong>Name:</strong></td>
<td><input name="StaffName" type="text" class="bodytext" id="StaffName" size="40"></td>
</tr>
<tr>
<td><strong>Title:</strong></td>
<td><input name="StaffTitle" type="text" class="bodytext" id="StaffTitle" size="40"></td>
</tr>
<tr>
<td><strong>Extension:</strong></td>
<td><input name="Extension" type="text" class="bodytext" id="Extension" size="10"></td>
</tr>
<tr>
<td><strong>Image:</strong></td>
<td><input name="StaffPhoto" type="file" class="bodytext" id="StaffPhoto" size="28">
<input name="MAX_FILE_SIZE" type="hidden" id="MAX_FILE_SIZE2" value="6000000"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input name="Upload" type="submit" id="Upload" value="Upload" class="bodytext"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>Code: Select all
<?php
if($HTTP_POST_VARS['Upload'] == "Upload"){
}
if($_POST['Upload'] == "Upload"){
$originalImageFileName = $_FILES['StaffPhoto']['name'];
$tempImageFileName = $_FILES['StaffPhoto']['tmp_name'];
$siteRoot = $_SERVER["DOCUMENT_ROOT"];
$uploadDir = $siteRoot . "/staffadmin/";
$newImagePath = "/staffadmin/photos/" . $originalImageFileName;
umask(0);
move_uploaded_file($tempImageFileName, $newImagePath);
system("chmod 755 $newPath");
//define query
$query = "UPDATE staff SET Category='{$_POST['Category']}', List_no='{$_POST['List_no']}', StaffName='{$_POST['StaffName']}', StaffTitle='{$_POST['StaffTitle']}', Extension='{$_POST['Extension']}', StaffPhoto='{$_FILES['StaffPhoto']['name']}' WHERE id={$_POST['id']}";
$r - mysql_query ($query); //execute query
//report result
if (mysql_affected_rows() == 1) {
echo '<p class=\"bodytext\">The staff member information has been successfully updated!</p>';
} else {
echo "<p class=\"bodytext\">The staff member information could not be updated because: <b>" . mysql_error() . "</b>.
The query was <b>$query</b>.</p>";
}
} else { //display event in form
// check for valid id in URL
if (is_numeric ($_GET['id'])) {
//define query
$query = "SELECT * FROM staff WHERE id={$_GET['id']}";
if ($r = mysql_query ($query)) { //run query
$row = mysql_fetch_array ($r); //retrieve information
//make form
echo '<form action="edit_staff.php" method="post" name="Upload" id="Upload">
<table>
<tr>
<td class="bodytext">Category: </td>
<td><input class="bodytext" type="text" name="Category" value="' . $row['Category'] . '" /></td>
</tr>
<tr>
<td class="bodytext">Order within Category:</td>
<td><input class="bodytext" type="text" name="List_no" value="' . $row['List_no'] . '" /></td>
</tr>
<tr>
<td class="bodytext">Name:</td>
<td><input class="bodytext" size="40" type="text" name="StaffName" value="' . $row['StaffName'] . '" /></td>
</tr>
<tr>
<td class="bodytext">Title:</td>
<td><input class="bodytext" size="40" type="text" name="StaffTitle" value="' . $row['StaffTitle'] . '" /></td>
</tr>
<tr>
<td class="bodytext">Extension:</td>
<td><input class="bodytext" size="40" type="text" name="Extension" value="' . $row['Extension'] . '" /></td>
</tr>
<tr>
<td class="bodytext">Photo:</td>
<td><input name="photo" type="file" id="photo" size="40" value="' . $row['StaffPhoto'] . '" />
<input name="MAX_FILE_SIZE" type="hidden" id="MAX_FILE_SIZE" value="6000000"></td>
</tr>
<tr>
<td colspan=\"2\" class="bodytext"><strong>Remember!</strong> Photos must be in JPG format and must be sized to 75 pixels wide by 110 pixels high prior to uploading.
</td>
</tr>
<tr>
<td colspan=\"2\" class="bodytext"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="hidden" name="id" value="' . $_GET['id'] . '" />
<input type="submit" name="Upload" class="bodytext" value="Upload" />
</td>
</tr>
</table>
</form>';
} else { //couldn't get information
echo "<p class=\"bodytext\">Couldn't retrieve the information because: <b>" . mysql_error() . "</b>. The query was <b>$query</b>.</p>";
}
} else { //no id set
echo '<p class=\"bodytext\">There was an error using this page. Please contact XXXX for assistance.</p>';
}
} //end main IF
mysql_close(); //close db connectionCan anyone help me figure out where I've gone wrong?