I need to update a PDF file that already has been uploaded for a certain user (using html form). I have added a code to update the PDF document (to choose another/new document) but it is not working. It is just updating the name of the file to the database without uploading the file to the folder and the path to the database like I have in my insert.php.
This is my insert.php code:
<?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);
?>
This is my update.php code:
<?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();
}
?>
And this is my edit.php code:
<?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
}
?>
Update
I have updated my code so it can be more readable and more simple. I am looking for help with updating the PDF file.