简体   繁体   English

在更新表单上上传PHP文件

[英]PHP File Upload on Update form

I have created a edit form as follows. 我创建了如下的编辑表单。 I want to edit the uploaded file and it should be updated in the mysql db. 我想编辑上传的文件,应该在mysql数据库中对其进行更新。 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 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 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;
}
?>

Move the file upload code to go inside the if($_POST) code block. 将文件上传代码移到if($ _ POST)代码块中。

You are not checking if the $_FILES array exists before running any code on it. 在运行任何代码之前,您无需检查$ _FILES数组是否存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM