I am trying to use a modal form to upload and retrieve PDF files. The form includes a file upload and descriptive text (Vendor Name, Category, and Notes) which gets sent to MySQL database through AJAX/PHP. I end up with a table with last column including a button (which I will want a click to open the PDF related to the row of text. The issue I am having is I can only upload to the database or upload the file when submitting the form, unable to do both at the same time. Can anyone provide thought based on the below code and images?
Also, I am uploading directly to the webserver.. is there a "best practice" when it comes to uploading file (i.e. different fileserver)?
<!-- The Modal/Form -->
<div class="modal-body">
<form method="post" action="upload-manager.php" id="insert_form" enctype="multipart/form-data">
<label>Vendor</label>
<input type="text" name="vendor" id="vendor" class="form-control" />
<br />
<label>Category</label>
<input type="text" name="category" id="category" class="form-control" />
<br />
<label>Notes</label>
<textarea name="notes" id="notes" class="form-control"></textarea>
<br />
<label>Date</label>
<input type="date" name="date" id="date" class="form-control"></textarea>
<script>
$('#date').val(new Date().toJSON().slice(0,10));
</script>
<br />
<label>Import File</label>
<input type="file" name="file" id="fileSelect" class="form-control-file" />
<br />
<input type="submit" name="insert" id="insert" value="Done" class="btn btn-success" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- PHP for uploading file to server -->
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
//file details
$name = $file['name'];
$tmp_name = $file['tmp_name'];
$tmp_file_name = "{$name}";
$tmp_file_path = "files/{$tmp_file_name}";
// mode the file
move_uploaded_file($tmp_name, $tmp_file_path);
}
?>
<!-- Script for sending to SQL database -->
<script>
$(document).ready(function(){
$('#insert_form').on("submit", function(event){
event.preventDefault();
if($('#vendor').val() == "")
{
alert("Vendor is required");
}
else if($('#category').val() == '')
{
alert("Categoy is required");
}
else if($('#date').val() == '')
{
alert("Date is required");
}
else
{
$.ajax({
url:"insert.php",
method:"POST",
data:$('#insert_form').serialize(),
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
$('#insert_form')[0].reset();
$('#add_data_Modal').modal('hide');
$('#dataTable').html(data);
$('#insert').val("Done");
$('#date').val(new Date().toJSON().slice(0,10));
}
});
}
});
insert.php
<?php
//insert.php
include_once 'db.php';
if(!empty($_POST))
{
$output = '';
$vendor = mysqli_real_escape_string($connect, $_POST["vendor"]);
$category = mysqli_real_escape_string($connect, $_POST["category"]);
$notes = mysqli_real_escape_string($connect, $_POST["notes"]);
$date = mysqli_real_escape_string($connect, $_POST["date"]);
$query = "
INSERT INTO pdfFiles(Vendor, Category, Notes, Date)
VALUES('$vendor', '$category', '$notes', '$date')
";
if(mysqli_query($connect, $query))
{
$output .= '<label class="text-success">Data Inserted</label>';
$select_query = "SELECT * FROM pdfFiles ORDER BY id DESC";
$result = mysqli_query($connect, $select_query);
$output .= '
<table class="table table-bordered">
<tr>
<th width="25%">Vendor Name</th>
<th width="25%">Category</th>
<th width="25%">Notes</th>
<th width="15%">Date</th>
<th width="10%">View</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>' . $row["Vendor"] . '</td>
<td>' . $row["Category"] . '</td>
<td>' . $row["Notes"] . '</td>
<td>' . $row["Date"] . '</td>
<td><input type="button" name="view" value="view" id="' . $row["ID"] . '" class="btn btn-info btn-xs view_data" /></td>
</tr>
';
}
$output .= '</table>';
}
echo $output;
}
?>



best practises- use Prepared Statements is a big one here as your code, despite the use ofmysqli_real_escape_stringis potentially vulnerable to sql injection.<input type="date" name="date" id="date" class="form-control"></textarea>is clearly incorrect<textarea>and I must have missed deleting the</textarea>Good catch!