I am trying to upload files to a given folder and to the database using PHP. Files are uploading to the folder successfully but don’t update the database. I can’t find the issue of that. Please help me to solve the problem.
JavaScript
x
<?php
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "abc_school";
//Create connection
$conn = mysqli_connect($host, $dbUsername, $dbPassword, $dbName);
// Uploads files
$targetDir = "uploads/";
$lessonNo = $_POST['lno'];
$lessonName = $_POST['lname'];
$description = $_POST['ldescription'];
$date = $_POST['ldate'];
$fileName = $_FILES['lfile']['name'];
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(isset($_POST["upload"]) && !empty($_FILES['lfile']['name'])){
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif','pdf');
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES['lfile']['tmp_name'], $targetFilePath)){
// Insert image file name into database
$insert = "INSERT INTO lessons (lesson_no, name, description, date, file) VALUES ($lessonNo, '$lessonName',
'$description', '$date', '$fileName');";
$result_insert = mysqli_query($conn,$insert);
if($insert){
$statusMsg = "The file ".basename($_FILES['lfile']['name']). " has been uploaded successfully.";
}
else{
$statusMsg = "File upload failed, please try again.";
}
}
else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
else{
$statusMsg = "Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.";
}
}
else{
$statusMsg = "Please select a file to upload.";
}
echo $statusMsg;
?>
This is a screenshot of the table that I am trying to update.
Advertisement
Answer
Are there no errors displayed?
Have you tried to display the contents of the $insert variable with a var_dump then copy / paste the SQL query into your mysql executor (ex: phpmyadmin)?
I think you want to test the $result_insert and not the $insert in this part:
JavaScript
if($insert){
$statusMsg = "The file ".basename($_FILES['lfile']['name']). " has been uploaded successfully.";
}
else{
$statusMsg = "File upload failed, please try again.";
}