I have a webpage users can upload documents with. Right now, the code can successfully upload the document to a directory on the server without any issues. However, I need to upload the document to a MySQL table as a new inserted row and then display the document as a link back on the original webpage. However, every time I try to upload to MySQL, it fails and I'm not sure why that is. I receive 0 errors in debugging mode and I can log in and connect successfully to the database. My Query is what fails, but I can successfully run the query in MySQL without errors.
MY CODE:
HTML:
<body>
<br/>
<div id="bodydiv">
<fieldset id='title'>
<span style='color:aliceblue'>Uploaded SG Documents</span>
</fieldset>
<br/>
<fieldset id='docTypeWO'>
<span>Scanned Work Orders:</span>
<div id='responseWO'>
</div>
</fieldset>
<br/>
<fieldset id='docTypeCS'>
<span>Cut Sheets:</span>
<div id='responseCS'>
</div>
</fieldset>
<br/>
<fieldset id='docTypeOther'>
<span>Others:</span>
<div id='responseOther'>
</div>
</fieldset>
<br/>
<form name="sgFileUpload" id="sgFileUpload" action='sg_addupload.php' method="POST" enctype="multipart/form-data">
<fieldset id='uploadBtnField'>
<input type="hidden" name="MAX_FILE_SIZE" value="50000000"/>
<input type='file' name='searchFile' id='searchFile' multiple>
<input type='submit' name='startUpload' id='startUpload' value='Upload'>
<!-- <input type='reset' name='cancelUpload' id='cancelUpload' value="Cancel Upload">
<input type='button' name='deleteFile' id='deleteFile' value='Delete'> -->
</fieldset>
<!-- The table listing the files available for upload/download -->
<table><tbody></tbody></table>
</form> <!-- End Form Input -->
</div>
</body>
</html>
My AJAX:
j('#startUpload').on('click', function() {
var file_data = j('#searchFile').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
alert(form_data);
j.ajax({
url: 'sg_addupload.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'POST',
success: function(data){
j('#responseWO').html(data); // display response from the PHP script, if any
}
});
});
My PHP:
include('inc.php');
//This section works successfully to upload to a directory on the server.
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
}
//This section fails...
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['file']['name']);
//This gets all the other information from the form
$fileName = basename( $_FILES['file']['name']);
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
//Writes the Filename to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded, and your information has been added to the directory";
//connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if(mysqli_connect_errno() ) {
printf('Could not connect: ' . mysqli_connect_error());
exit();
}
$conn->select_db($dbname);
if(! $conn->select_db($dbname) ) {
echo 'Could not select database. '.'<BR>';
}
//Writes the information to the database
mysqli_query("INSERT INTO sg_uploads(sgref,file,type,size,content,doctype) VALUES('4','$fileName','$fileType','$fileSize','$content','Other')");
} else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your file.";
}
All help is appreciated. Thank you!
mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you will create severe SQL injection bugs. NEVER put$_POSTdata directly into a query.$...type values in your query with?and then usebind_paramwith the appropriate type. It takes literally ten minutes to figure out and will save you hours and hours of frustrating debugging, and possibly even your career if it keeps your site secure.