-2

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.

<?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.

screenshot

  • Check your `mysqli_query` for errors you can find the error message with `mysqli_error ` at the bottom of the this page you can find an example of how it is done, https://www.w3schools.com/php/func_mysqli_error.asp – Breezer Nov 17 '21 at 16:10
  • What error are you getting? Also you are vulnerable to SQL injection. Use parameterized queries instead. – Mark Baijens Nov 17 '21 at 16:11
  • Thank you @Breezer for your solution. It worked and I found the error. – Tashini Somarathne Nov 17 '21 at 16:30
  • @TashiniSomarathne glad I could help – Breezer Nov 17 '21 at 16:31
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 17 '21 at 18:24

1 Answers1

-2

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:

        if($insert){
            $statusMsg = "The file ".basename($_FILES['lfile']['name']). " has been uploaded successfully.";
        }
        else{
            $statusMsg = "File upload failed, please try again.";
        } 
CecileV
  • 16
  • 2
  • No. Its not working. Then it displays "File upload failed, please try again." when testing $result_insert. Thank you the help. – Tashini Somarathne Nov 17 '21 at 16:24
  • @TashiniSomarathne yeah, you should display in the else branch the real error message as Breezer suggested in a comment. However, CecileV was correct suggesting that you test the wrong variable for error. – Shadow Nov 17 '21 at 16:42