如何更新已经在PHP中添加到数据库的PDF文件?

时间:2018-12-22 21:18:22

标签: php html mysql mysqli

我需要更新已经为特定用户上传的PDF文件(使用html表单)。我添加了代码来更新PDF文档(以选择另一个/新文档),但是它不起作用。它只是将文件名更新到数据库,而没有像我在insert.php中一样将文件上传到文件夹和数据库路径。

这是我的insert.php代码:

<?php
$server = "localhost";
$user = "root";
$pass = "";
$dbname = "employees";

// Create connection
$conn = mysqli_connect($server, $user, $pass, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$dob = mysqli_real_escape_string($conn, $_POST['dob']);
$embg = mysqli_real_escape_string($conn, $_POST['embg']);
$address = mysqli_real_escape_string($conn, $_POST['address']);
$city = mysqli_real_escape_string($conn, $_POST['city']);
$mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$workplace = mysqli_real_escape_string($conn, $_POST['workplace']);
$workposition = mysqli_real_escape_string($conn, $_POST['workposition']);
$jobstartdate = mysqli_real_escape_string($conn, $_POST['jobstartdate']);
$contractfrom = mysqli_real_escape_string($conn, $_POST['contractfrom']);
$contractto = mysqli_real_escape_string($conn, $_POST['contractto']);
$healthbookfrom = mysqli_real_escape_string($conn, $_POST['healthbookfrom']);
$healthbookto = mysqli_real_escape_string($conn, $_POST['healthbookto']);
$bankaccount = mysqli_real_escape_string($conn, $_POST['bankaccount']);
$bank = mysqli_real_escape_string($conn, $_POST['bank']);
$workcode = mysqli_real_escape_string($conn, $_POST['workcode']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);
$bloodtype = mysqli_real_escape_string($conn, $_POST['bloodtype']);
$notes = mysqli_real_escape_string($conn, $_POST['notes']);
$contract_file = basename($_FILES['contractupload']['name']);
$contract_path = "files/contracts/$contract_file";
$contract_file = mysqli_real_escape_string($conn, $contract_file);

copy($_FILES['contractupload']['tmp_name'], $contract_path);  // copy the file to the folder


$sql = "INSERT INTO addemployees (fname, lname, dob, embg, address, city, mobile, email, workplace, workposition, jobstartdate, contractfrom, contractto, healthbookfrom,
                                  healthbookto, contractupload, bankaccount, bank, workcode, gender, bloodtype, notes)
        VALUES ('$fname', '$lname', '$dob', '$embg', '$address', '$city', '$mobile', '$email', '$workplace', '$workposition', '$jobstartdate', '$contractfrom', '$contractto',
                '$healthbookfrom', '$healthbookto', '$contract_file', '$bankaccount', '$bank', '$workcode', '$gender', '$bloodtype', '$notes')";

if (mysqli_query($conn, $sql)) {
  header("location: employees.php");
    // echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


// Use this to write text for the connection ---> echo "Connected successfully";

//Close the connection
mysqli_close($conn);    
?>

这是我的update.php代码:

       <?php

// Include config file
require_once "new_db_connect.php";

if($_POST) {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $embg = $_POST['embg'];
    $contractupload = $_POST['contractupload'];
    $contract_file = $_FILES['contractupload']['name'];
    $contract_path = "files/contracts/$contract_file";

copy($_FILES['contractupload']['tmp_name'], $contract_path);

    $id = $_POST['id'];

// UPDATE the info
    $sql = "UPDATE addemployees SET fname = '$fname', lname = '$lname', embg = '$embg', contractupload = '$contractupload' WHERE id = {$id}";
    if($connect->query($sql) === TRUE) {
        header("location: employees.php");

    } else {
        echo "Erorr while updating record : ". $connect->error;
    }

    $connect->close();

}

?>

这是我的edit.php代码:

<?php

// Include config file
require_once "new_db_connect.php";

if($_GET['id']) {
    $id = $_GET['id'];

    $sql = "SELECT * FROM addemployees WHERE id = {$id}";
    $result = $connect->query($sql);

    $data = $result->fetch_assoc();

    $connect->close();

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Update Record</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        .wrapper{
            width: 500px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header">
                        <h2>Update User Info</h2>
                    </div>
                    <form action="update.php" method="post">

                      <div class="form-group">
                          <label>Name</label>
                          <input type="text" id="fname" name="fname" class="form-control" value="<?php echo $data['fname'] ?>">
                      </div>

                      <div class="form-group">
                          <label>Last Name</label>
                          <input type="text" id="lname" name="lname" class="form-control" value="<?php echo $data['lname'] ?>">
                      </div>

                      <div class="form-group">
                          <label>ID Number</label>
                          <input type="text" id="embg" name="embg" class="form-control" value="<?php echo $data['embg'] ?>">
                      </div>

                      <div class="form-group">
                          <label>Contract PDF</label>
                          <input type="file" name="contractupload" id="contractupload" class="form-control" style="border: 1px solid #CED4DA!important;" style="width: 50%!important;" value="<?php echo $data['contractupload'] ?>">
                      </div>

                      <input type="hidden" name="id" value="<?php echo $data['id'] ?>"/>
                      <input type="submit" class="btn btn-primary" value="Submit">
                      <a href="employees.php" class="btn btn-default">Cancel</a>
                  </form>

                </div>
            </div>
        </div>
    </div>
</body>
</html>

<?php
}
?>

更新

我已经更新了我的代码,因此它可以更易读和更简单。我正在寻求有关更新PDF文件的帮助。

1 个答案:

答案 0 :(得分:1)

$_POST['contractupload']不起作用。文件名仅在$_FILES中。您应该像在insert.php中一样进行处理。

我还展示了如何使用准备好的语句而不是变量替换来重写代码。

您应该使用move_uploaded_file()而不是copy()。参见Difference between copy and move_uploaded_file

<?php

// Include config file
require_once "new_db_connect.php";

if($_POST) {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $embg = $_POST['embg'];
    $contract_file = basename($_FILES['contractupload']['name']);
    $contract_path = "files/contracts/$contract_file";

    move_uploaded_file($_FILES['contractupload']['tmp_name'], $contract_path);

    $id = $_POST['id'];

    // UPDATE the info
    $stmt = $connect->prepare("UPDATE addemployees SET fname = ?, lname = ?, embg = ?, contractupload = ? WHERE id = ?");
    $stmt->bind_param("ssssi", $fname, $lname, $embg, $contract_file, $id);
    if($stmt->execute()) {
        header("location: employees.php");
    } else {
        echo "Erorr while updating record : ". $stmt->error;
    }

    $connect->close();

}

?>