Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding uploading to edit page
    text
    copied!<p>I have been trying the whole week to get this too work but haven't had any luck thus far. I am building an employee system, being my first project I could really use your help.</p> <p>I have a database with a table called <code>ref_employees</code> with x amount of fields.</p> <p>I managed to get my hands on some source to edit the record and thought that my problem was solved. Although the source helped me to edit the records, the client needs more functionality by means of upload and storing functionality. I have edited the code accordingly but have 2 issues now. </p> <p>1) I had to add the upload form separate to the editing form because when the edits' update is clicked it clears the upload fields within the db even after adding echoing out the current values within the upload fields in the db.</p> <p>2) The uploads shows that it is uploading but is doesn't get saved in the specified directory. The permissions are set to 777, and the file names are not captured in the database in the relevant fields. I think it is because the upload function is in a separate page and not on the same page as the upload form. </p> <p>I need it to upload the file, store it in a directory and finally place the file name in the db where the warning fields are, but it needs to be captured under the record (employee) being edited.</p> <p>I am new to this and all help is appreciated.</p> <p>The edit page:</p> <pre><code> &lt;?php include 'core/init.php'; protect_page(); include 'includes/overall/header.php'; error_reporting(1); ?&gt; &lt;?php /* EDIT.PHP Allows user to edit specific entry in database */ // creates the edit record form // since this form is used multiple times in this file, I have made it a function that is easily reusable function renderForm($idnumber, $firstname, $lastname, $department, $manager, $startdate, $error) { ?&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Edit Record&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="article"&gt; &lt;h1&gt;Employee Details&lt;/h1&gt; &lt;?php // if there are any errors, display them if ($error != '') { echo '&lt;div style="padding:4px; border:1px solid red; color:red;"&gt;'.$error.'&lt;/div&gt;'; } ?&gt; &lt;form action="" method="post" enctype="multipart/form-data"&gt; &lt;input type="hidden" name="idnumber" value="&lt;?php echo $idnumber; ?&gt;"/&gt; &lt;div&gt; &lt;p&gt;* Required&lt;/p&gt; &lt;p&gt;&lt;strong&gt;ID:&lt;/strong&gt; &lt;?php echo $idnumber; ?&gt;&lt;/p&gt; &lt;table cellpadding="5" cellspacing="5"&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;First Name: *&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="firstname" value="&lt;?php echo $firstname; ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;Last Name: *&lt;/strong&gt;&lt;/td&gt; &lt;td&gt; &lt;input type="text" name="lastname" value="&lt;?php echo $lastname; ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;Department: *&lt;/strong&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="department" value="&lt;?php echo $department; ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;Manager/Superviser: *&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="manager" value="&lt;?php echo $manager; ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;Start Date: *&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="startdate" value="&lt;?php echo $startdate; ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="submit" name="submit" value="Submit" class="btn"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;tr&gt; &lt;td&gt; &lt;table cellpadding="5" cellspacing="0"&gt; &lt;form action="includes/add.php" method="post" enctype="multipart/form-data"&gt; &lt;input type="hidden" name="idnumber" value="&lt;?php echo $idnumber; ?&gt;"/&gt; &lt;th&gt;Ad Warnings Documents&lt;/th&gt; &lt;tr&gt; &lt;td&gt;Warning File 1&lt;/td&gt; &lt;td&gt;&lt;input type="file" name="warning1" value="&lt;?php echo $warning1;?&gt;" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Warning File 2&lt;/td&gt; &lt;td&gt;&lt;input type="file" name="warning2" value="&lt;?php echo $warning2;?&gt;" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Warning File 3&lt;/td&gt; &lt;td&gt;&lt;input type="file" name="warning3" value="&lt;?php echo $warning3;?&gt;" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;input type="submit" name="submit" value="upload"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php } // check if the form has been submitted. If it has, process the form and save it to the database if (isset($_POST['submit'])) { // confirm that the 'id' value is a valid integer before getting the form data if (is_numeric($_POST['idnumber'])) { // get form data, making sure it is valid $idnumber = $_POST['idnumber']; $firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname'])); $lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname'])); $department = mysql_real_escape_string(htmlspecialchars($_POST['department'])); $manager = mysql_real_escape_string(htmlspecialchars($_POST['manager'])); $startdate = mysql_real_escape_string(htmlspecialchars($_POST['startdate'])); // check that firstname/lastname fields are both filled in if ($firstname == '' || $lastname == '') { // generate error message $error = 'ERROR: Please fill in all fields!'; //error, display form renderForm($idnumber, $firstname, $lastname, $department, $manager, $startdate, $error); } else { // save the data to the database mysql_query("UPDATE ref_employees SET firstname='$firstname', lastname='$lastname', department='$department', manager='$manager', startdate='$startdate' WHERE idnumber='$idnumber'") or die(mysql_error()); // once saved, redirect back to the view page header("Location: employeelist.php"); } } else { // if the 'id' isn't valid, display an error echo 'Error!'; } } else // if the form hasn't been submitted, get the data from the db and display the form { // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) if (isset($_GET['idnumber']) &amp;&amp; is_numeric($_GET['idnumber']) &amp;&amp; $_GET['idnumber'] &gt; 0) { // query db $idnumber = $_GET['idnumber']; $result = mysql_query("SELECT * FROM ref_employees WHERE idnumber=$idnumber") or die(mysql_error()); $row = mysql_fetch_array($result); // check that the 'id' matches up with a row in the databse if($row) { // get data from db $firstname = $row['firstname']; $lastname = $row['lastname']; $department = $row['department']; $manager = $row['manager']; $startdate = $row['startdate']; $warning1 = $row['warning1']; $warning2 = $row['warning2']; $warning3 = $row['warning3']; // show form renderForm($idnumber, $firstname, $lastname, $department, $manager, $startdate, ''); } else // if no match, display result { echo "No results!"; } } else // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error { echo 'Error!'; } } ?&gt; &lt;h1&gt;Additional options&lt;/h1&gt; &lt;/div&gt; </code></pre> <p>The file upload source file add.php</p> <pre><code>&lt;?php include 'core/init.php'; protect_page(); include 'includes/overall/header.php'; error_reporting(1); ?&gt; &lt;?php //This is the directory where images will be saved $target = "files/empdocs"; $target1 = $target . basename( $_FILES['warning1']['name']); $target2 = $target . basename( $_FILES['warning2']['name']); $target3 = $target . basename( $_FILES['warning3']['name']); //This gets all the other information from the form $warning1=($_FILES['warning1']['name']); $warning2=($_FILES['warning2']['name']); $warning3=($_FILES['warning3']['name']); //Writes the information to the database mysql_query("INSERT INTO ref_employees VALUES ('$warning1', '$warning2', '$warning3')") ; //Writes the file to the server if (move_uploaded_file($_FILES['warning1']['tmp_name'], $target1) &amp;&amp; move_uploaded_file($_FILES['warning2']['tmp_name'], $target2) &amp;&amp; move_uploaded_file($_FILES['warning3']['tmp_name'], $target3)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload