update clears image filename from db table when when editing record file upload php mysql
Hi all,
So I'm trying to edit a staff members photo in the database I have created. There are three browse buttons for different documents to upload.
I can add the photo and save it in a location and it stays there. But if I want to edit any of the text boxes but keep the photo of the staff member it will delete the photo.
I am using DW's built in Update routine and then a separate php file to do the image processing, do you think it could be happening because of this?
This is the UPDATE query:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
include('staffimageprocessing.php');
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE sure_staff SET sure_staff_fname=%s, sure_staff_sname=%s, sure_staff_telno=%s, staff_photo=%s, staff_photoid=%s, staff_dbs=%s, sure_staff_email=%s, sure_staff_password=%s, sure_staff_active=%s WHERE sure_staff_id=%s",
GetSQLValueString($_POST['fname'], "text"),
GetSQLValueString($_POST['sname'], "text"),
GetSQLValueString($_POST['telno'], "text"),
GetSQLValueString($_FILES['pic']['name'], "text"),
GetSQLValueString($_FILES['photoid']['name'], "text"),
GetSQLValueString($_FILES['dbs']['name'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['password'], "text"),
GetSQLValueString($_POST['active'], "text"),
GetSQLValueString($_POST['staffid'], "int"));
mysql_select_db($database_db, $db);
$Result1 = mysql_query($updateSQL, $db) or die(mysql_error());
$updateGoTo = "staff.php";This is the Form:
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1">
<table width="100%" border="0" cellpadding="2" cellspacing="2" class="tabledisplay">
<tr>
<th width="14%" scope="row">First Name</th>
<td width="2%"> </td>
<td width="84%"><input name="fname" type="text" id="fname" value="<?php echo $row_loadcasenotes['sure_staff_fname']; ?>"></td>
</tr>
<tr>
<th scope="row">Last Name</th>
<td> </td>
<td><span id="tenantid"><span class="selectRequiredMsg">Please select an item.</span>
<input name="sname" type="text" id="sname" value="<?php echo $row_loadcasenotes['sure_staff_sname']; ?>">
</span></td>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
<td> </td>
</tr>
<tr>
<th scope="row">Telephone</th>
<td> </td>
<td><input name="telno" type="text" id="telno" value="<?php echo $row_loadcasenotes['sure_staff_telno']; ?>"></td>
</tr>
<tr>
<th scope="row">Email</th>
<td> </td>
<td><input name="email" type="text" id="email" value="<?php echo $row_loadcasenotes['sure_staff_email']; ?>" size="50"></td>
</tr>
<tr>
<th scope="row">Password</th>
<td> </td>
<td><input name="password" type="password" id="password" value="<?php echo $row_loadcasenotes['sure_staff_password']; ?>"></td>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
<td> </td>
</tr>
<tr>
<th scope="row">Photo</th>
<td> </td>
<td><input type="file" name="pic" id="pic" value="<?php echo $row_loadcasenotes['staff_photo']; ?>">
<a href="../documents/staff/photoid/<?php echo $row_loadcasenotes['staff_photo']; ?>" target="_blank">View File</a></td>
</tr>
<tr>
<th scope="row">Photo ID</th>
<td> </td>
<td><input type="file" name="photoid" id="photoid" value="<?php echo $row_loadcasenotes['staff_photoid']; ?>">
<a href="../documents/staff/photoid/<?php echo $row_loadcasenotes['staff_photoid']; ?>" target="_blank">View File</a></td>
</tr>
<tr>
<th scope="row">DBS</th>
<td> </td>
<td><input type="file" name="dbs" id="dbs" value="<?php echo $row_loadcasenotes['staff_dbs']; ?>">
<a href="../documents/staff/dbs/<?php echo $row_loadcasenotes['staff_dbs']; ?>" target="_blank">View File</a></td>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
<td><select name="active">
<option value="YES" <?php if (!(strcmp("YES", $row_loadcasenotes['sure_staff_active']))) {echo "selected=\"selected\"";} ?>>YES</option>
<option value="NO" <?php if (!(strcmp("NO", $row_loadcasenotes['sure_staff_active']))) {echo "selected=\"selected\"";} ?>>NO</option>
</select>
<input name="staffid" type="hidden" id="staffid" value="<?php echo $row_loadcasenotes['sure_staff_id']; ?>"></td>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
<td><input type="submit" name="addstaff" id="addstaff" value="Add Staff"></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form1">
</form>
And this is the image processing file:
if(!empty($_FILES['pic']['name'])) {
if(isset($_FILES['pic']['name'])){
$errors= array();
$file_name = $_FILES['pic']['name'];
$file_size = $_FILES['pic']['size'];
$file_tmp = $_FILES['pic']['tmp_name'];
$file_type = $_FILES['pic']['type'];
$file_ext=strtolower(end((explode('.',$_FILES['pic']['name']))));
$extensions= array("jpeg","jpg","png","pdf","doc","docx");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG,PNG,PDF,DOC or DOCX file.";
}
if($file_size > 6291456) {
$errors[]='File size must be smaller than 5 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"../documents/staff/photoid/".$file_name);
// echo "Success";
}else{
// print_r($errors);
}
}
}As you can see it all seems fine.
I am even checking if there is anything in the input.
Am I placing the image processing file in the wrong place. I can't work it out. I've watched a few videos on YT and they point to the file in the form and then create the image processing file to include the update command, but if I was to do this for my other pages it would be crazy because 2 of the other pages have more than 15 file upload controls.
