我需要更新一个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条答案
按热度按时间k7fdbhmy1#
$_POST['contractupload']
不起作用。文件名仅在$_FILES
. 你应该用同样的方法处理它insert.php
.我还演示了如何使用准备好的语句而不是变量替换来重写代码。
你应该用
move_uploaded_file()
而不是copy()
. 请参阅复制和移动上传的文件之间的区别。