Hi all,
I am new to php, I am just learning it at the moment. I hope you can help me
with my code.
Thanks in advance!
I want the page to allow users to edit or delete an existing uploaded file.
But after testing the script in the browser the delete option is working 10
out 0f 10;
but the edit is not. I don't know what I am doing wrong.
Here's my code:
<?php # edit_file.php
// This page allows users to edit or delete existing URL records.
$page_title = 'Edit a file';
include ('./includes/header.html');
// Check for a URL ID.
if (isset($_GET['uid'])) { // Page is first accessed.
$uid = (int) $_GET['uid'];
} elseif (isset($_POST['uid'])) { // Form has been submitted.
$uid = (int) $_POST['uid'];
} else { // Big problem.
$uid = 0;
}
//<!-- BEGIN if ($uid <= 0)-->
if ($uid <= 0) { // Do not proceed!
echo '<p><font color="red">This page has been accessed
incorrectly!</font></p>';
include ('./includes/footer.html');
exit(); // Terminate execution of the script.
}//<!-- END if ($uid <= 0)-->
require_once ('./mysql_connect.php'); // Connect to the database.
$counter=1;
$name= $_FILES[$filename]['name'];
$size=$_FILES[$filename]['size'];
$type=$_FILES[$filename]['type'];
$temp_name=$_FILES[$filename]['temp_name'];
$error=$_FILES[$filename]['error'];
//<!-- BEGIN if (isset($_POST['submitted']))-->
if (isset($_POST['submitted'])) { // Handle the form.
//<!-- BEGIN if ($_POST['which'] == 'delete')-->
if ($_POST['which'] == 'delete') { // Delete the record.
// This part is working fine.
} else { // Edit the uploaded file .
for ($i = 0; $i < $counter; $i++) { // Handle the uploaded file.
$filename = 'upload' . $i;
$description = 'description' . $i;
// Check for a file.
if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 2)) {
// Check for a description
if (!empty($_POST[$description])) {
$d = "'" . escape_data($_POST [$description]) . "'";
} else {
$d = 'NULL';
}
// Update the record to the database.
$query = "UPDATE uploads SET file_name='$name',
file_size='$size', file_type='$type', description='$d' WHERE
upload_id=$uid";
$result = mysql_query ($query);
if ($result) {
// Return the upload_id from the database.
$upload_id = mysql_insert_id();
// Move the file over.
if (move_uploaded_file($_FILES[$filename]['tmp_name'],
"uploads/$upload_id")) {
echo '<p>File number ' . ($i + 1) . ' has been
uploaded!</p>';
} else { // File could not be moved.
echo '<p><font color="red">File number ' .
($i + 1) . ' could not be moved.</font></p>';
// Remove the record from the database.
$query = "DELETE FROM uploads WHERE upload_id = $upload_id";
$result = mysql_query ($query);
}//end move_uploaded_file
} else { // If the query did not run OK
echo '<p><font color="red">Your
submission could not be processed due to a system error. We apologize
for any inconvenience.</font></p>';
}//end if(result)
} // End of if (isset($the_file)...
} // End of FOR loop.
} // End of Edit/Delete if-else.
} // End of the main submitted conditional.
// --------- DISPLAY THE FORM ---------
//Retrieve the uploads's current information.
$query = "SELECT file_name, ROUND(file_size/1024) AS fs, file_type,
description, DATE_FORMAT(date_entered, '%e %M %Y') AS d FROM uploads
WHERE upload_id=$uid";
$result = mysql_query ($query);
//get all of the information first record
list($file_name, $fs, $file_type, $d, $de)= mysql_fetch_array ($result,
MYSQL_NUM);
?>
<form action="edit_file.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<fieldset><legend>Edit a File:</legend>
<p><b>Select One:</b> <input type="radio" name="which" value="edit"
checked="checked" /> Edit <input type="radio" name="which"
value="delete" />
Delete</p>
<?php
//Retrieve the uploads's current information.
$query = "SELECT file_name, ROUND(file_size/1024) AS fs, file_type,
description, DATE_FORMAT(date_entered, '%e %M %Y') AS de FROM uploads
WHERE upload_id=$uid";
$result = mysql_query ($query);
//print the current file's information in the browser
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<table border="0" width="100%" cellspacing="3" cellpadding="3"
align="center">
<tr>
<td align="left" width="20%"><font size="+1">File Name</font></td>
<td align="center" width="20%"><font size="+1">File Size</font></td>
<td align="left" width="20%"><font size="+1">Upload Date</font></td>
</tr>';
// Display each record.
echo " <tr>
<td align=\"left\">{$row['file_name']}</a></td>
<td align=\"center\">{$row ['fs']}kb</td>
<td align=\"left\">{$row ['de']}</td>
</tr>\n";
}
?>
<?php // Create the inputs.
for ($i = 0; $i < $counter; $i++) {
echo '<p><b>File:</b> <input type="file" name="upload' . $i . '"
/></p>
<p><b>Description:</b> <textarea
name="description' . $i . '" cols="40"
rows="5">'.$d.'</textarea></p><br />';
}
?>
</fieldset>
<input type="hidden" name="submitted" value="TRUE" />
<?php // Store the required hidden values.
echo '
<input type="hidden" name="uid" value="' . $uid . '" />
';
?>
<div align="center"><input type="submit" name="submit" value="Submit"
/></div>
</form>
<?php mysql_close(); // Close the database connection.
include ('./includes/footer.html');
?>
<?php // Create the inputs.
for ($i = 0; $i < $counter; $i++) {
echo '<p><b>File:</b> <input type="file" name="upload' . $i . '"
/></p>
<p><b>Description:</b> <textarea
name="description' . $i . '" cols="40"
rows="5">'.$d.'</textarea></p><br />';
}
?>
</fieldset>
<input type="hidden" name="submitted" value="TRUE" />
<?php // Store the required hidden values.
echo '
<input type="hidden" name="uid" value="' . $uid . '" />
';
?>
<div align="center"><input type="submit" name="submit" value="Submit"
/></div>
</form>
<?php mysql_close(); // Close the database connection.
include ('./includes/footer.html');
?>
Thanks in advance!