I'm trying to extract information from a web form directly into an existing .xlsm file. However, I've been unsuccessful in doing so, the file always ends up becoming corrupted. The web form takes two text inputs plus one file upload per user, after which the user may download the uploaded file with the given text inputs written into it.
The web form is written in HTML, with minimal JavaScript, and PHP, with the data inputs stored in a MySQL database.
ETA: Code is as follows. Mostly done by the person I am posting this on behalf of. For the download portion, one version of the code used this as reference: https://thisinterestsme.com/php-export-excel/ - however, the data could only be written to a new .xls file.
HTML form
<!-- Start My Account -->
<div class="my-account-box-main">
<div class="container">
<div class="my-account-page">
<div class="row">
<div class="col-lg-4 col-md-12">
<div class="account-box">
<div class="service-box">
<div class="service-desc">
<h4><b>Client Information</b></h4>
<div id="error"></div>
<br/>
<form name="client" method="post" action="php/SignUp.php" onsubmit="return validateInputs()">
<ul>
<li style="text-align:left">
<label for="Name">
<b>Company Name</b>
</label>
</li>
<li>
<input class="form-control" name="name_1" type="text" required="required" id="name_1" autocomplete="off">
</li>
<br/>
<li style="text-align:left">
<label for="Name">
<b>Company UEN</b>
</label>
</li>
<li style="text-align:left">
<input class="form-control" name="uen_1" type="text" required="required" id="uen_1" autocomplete="off">
</li>
<br/>
<li style="text-align:left">
<label for="Name">
<b>Confirm Company Name</b>
</label>
</li>
<li>
<input class="form-control" name="name_2" type="text" required="required" id="name_2" autocomplete="off">
</li>
<br/>
<li style="text-align:left">
<label for="Name">
<b>Confirm Company UEN</b>
</label>
</li>
<li>
<input class="form-control" name="uen_2" type="text" required="required" id="uen_2" autocomplete="off">
</li>
<br/>
<li style="float:center">
<input class="btn btn-success" name="Register" type="submit" id="Register" value="Sign Up">
</li>
</ul>
</form>
</div>
</div>
</div>
</div>
<div class="col-lg-4 col-md-12">
<div class="account-box">
<div class="service-box">
<div class="service-desc">
<h4><b>Admin Site (Example For testing)</b></h4>
<ul>
<li> <form name="admin" method="post" action="php/Admin.php" enctype="multipart/form-data"> </li>
<li style="text-align:left"> <label for="Name" ><b>Admin Name:</b></label>
<label display="inline-block"; style="text-align:left"><b>Admin (Example)</b></label></li>
<li style="text-align:left"> <label for="Name" ><b>Admin Role:</b></label>
<label display="inline-block"; style="text-align:left"><b>Website Designer (Example)</b></label></li>
<li style="text-align:left"> <label for="Name"><b>Template file selection</b></label> </li>
<li> <input type="file" name="file" id="file" required="required"></li>
<!--accept=".xls,.xlsx,.xlsm"-->
<br>
<li style="float:center"><input class="btn btn-success" name="Register" type="submit" id="Register" value="Upload"></li>
<li> </form> </li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-lg-4 col-md-12">
<div class="account-box">
<div class="service-box">
<div class="service-desc">
<h4><b>Download Link (Example For testing)</b></h4>
<form name="client" method="post" action="php/Download.php">
<ul>
<li style="text-align:left">
<label for="link" >
<b>Please enter Company Name and Company EUN for template download: (Example)</b>
</label>
</li>
<li style="text-align:left">
<label for="Name" >
<b>Company Name</b>
</label>
</li>
<li>
<input class="form-control" name="Name" type="text" required="required" id="Name" autocomplete="off">
<br/>
</li>
<li style="text-align:left">
<label for="Name">
<b>Company UEN</b>
</label>
</li>
<li>
<input class="form-control" name="UEN" type="text" required="required" id="UEN" autocomplete="off">
</li>
<br/>
<li style="float:center">
<input class="btn btn-success" name="Register" type="submit" id="Register" value="Download">
</li>
<!--<li><a href="Testing.php" style="text-align:center"><b>Super Software - Tax Tool (Example)</b></a> </br></li>-->
</ul>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- End My Account -->
SignUp.php
<?php
include_once 'dbs.Connect.php';
$get_pro = "SELECT * FROM users";
$run_pro = mysqli_query($conn,$get_pro);
while ($row_pro = mysqli_fetch_array($run_pro)) {
$z = $row_pro['user_ID'];
}
$ID = $z+1;
$name_1 = "";
//$name_2 = "";
$uen_1 = "";
//$uen_2 = "";
$db = mysqli_connect('localhost', 'root', '', 'testing');
if (isset($_POST['Register'])) {
// receive all input values from the form
$name_1 = mysqli_real_escape_string($db, $_POST['name_1']);
//$name_2 = mysqli_real_escape_string($db, $_POST['name_2']);
$uen_1 = mysqli_real_escape_string($db, $_POST['uen_1']);
//$uen_2 = mysqli_real_escape_string($db, $_POST['uen_2']);
$Name = $name_1;
$UEN = $uen_1;
$queryResult = $conn->query("INSERT INTO users(user_ID,user_Name,user_UEN,user_File) VALUES ('$ID','$Name','$UEN',(select File from admin))");
if ($queryResult == TRUE) {
header('Location:../my-account.html');
}
}
mysqli_Close($conn)
?>
Download.php (successfully downloads the original uploaded file)
<?php
include_once 'dbs.Connect.php';
$Name =mysqli_real_escape_string($conn,$_POST['Name']);
$UEN = mysqli_real_escape_string($conn,$_POST['UEN']);
$result = $conn->query("SELECT * FROM users WHERE user_Name='$Name' AND user_UEN='$UEN'");
$row = mysqli_fetch_assoc($result);
session_start();
$_SESSION["Name"] = $row['user_Name'];
$_SESSION["UEN"] = $row['user_UEN'];
if( isset($_SESSION['Name']) && isset($_SESSION['UEN'])){
$result = "SELECT * FROM admin WHERE ID = '1'";
$run = mysqli_query($conn,$result);
$row = mysqli_fetch_assoc($run);
$file = $row['File'];
$filepath = "../php/" . $file;
// Process download
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit;
}
}
else{
echo "<script>
alert('Please key in the correct company name and company UEN.');
window.location.href='../my-account.html';
</script>";
}
mysqli_Close($conn)
?>