I have created a edit form as follows. I want to edit the uploaded file and it should be updated in the mysql db. While inserting a record file is uploading into the db but while editing a record its not uploading into db. Remaining fields are updating but file upload is not happening.
Can anybody help me to solve this issue?
Thanks in advance.
Edit.php
<?php
// include db connection.
include 'dbconn.php';
// If the form was submitted/posted, update the record.
if($_POST)
{
$path = '';
$folder = "Folder/";
if (is_uploaded_file($_FILES['filename']['tmp_name']))
{
if (move_uploaded_file($_FILES['filename']['tmp_name'], $folder.$_FILES['filename']['name']))
{
$path = $folder . $_FILES['filename']['name'];
}
else
{
$path = '';
};
}
else
{
$path = '';
};
// write query.
$sql = "UPDATE main SET category = ?, sd = ?, fd = ?, assignto = ?, reviewed = ?, upload = ? WHERE srn = ?";
$stmt = $mysqli->prepare($sql);
// Binding params.
$stmt->bind_param('sssssbi',$_POST['category'],$_POST['sd'],$_POST['fd'],$_POST['assignto'],$_POST['reviewed'],$_POST['path'],$_POST['srn']);
// Execute the update statement.
if($stmt->execute())
{ ?>
<script language="javascript">
alert("Task updated successfully");
top.location.href = "view.php"; // Page redirection.
</script>
// Close the prepared statement.
<?php $stmt->close();
}
else
{
die("Unable to update the task....");
}
}
$sql = "SELECT srn, client, type, fy, category, sd, fd, assignto, edoc, reviewed, upload FROM main WHERE srn = \"" . $mysqli->real_escape_string($_GET['srn']) . "\" LIMIT 0,1";
// Execute the sql query.
$result = $mysqli->query($sql);
// Get the result.
$row = $result->fetch_assoc();
// php's extract() makes $row['client'] to $client automatically.
extract($row);
// Disconnect from db.
$result->free();
$mysqli->close();
?>
<form action="Edit.php?srn=<?php echo $srn; ?>" method="POST" enctype="multipart/form-data" novalidate>
<span>File upload</span>
<input type="hidden"name="MAX_FILE_SIZE" value="2000">
<input name ="filename" type="file"/>
<button id='send' type='submit'>Update</button>
</form>
dbconn.php
<?php
// Set connection variables.
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
// Connect to mysql server
$mysqli = new mysqli($host,$user,$pwd,$db);
/* Check if any error occured */
if (mysqli_connect_errno())
{
echo "Failed to connect to mysql : " . mysqli_connect_error();
exit;
}
?>