0

I have tried to use php in order to upload files from web to phpMyAdmin.
I used the code that is attached below:

<!DOCTYPE html>
<html>
<head>
    <title>File Upload</title>
</head>
<body>
 
<form method="post" enctype="multipart/form-data">
    <label>Title</label>
    <input type="text" name="title">
    <label>File Upload</label>
    <input type="File" name="file">
    <input type="submit" name="submit">
 
 
</form>
 
</body>
</html>
 
<?php 
$localhost = "localhost"; #localhost
$dbusername = "root"; #username of phpmyadmin
$dbpassword = "";  #password of phpmyadmin
$dbname = "CodeFlix";  #database name
 
#connection string
$conn = mysqli_connect($localhost,$dbusername,$dbpassword,$dbname);
 
if (isset($_POST["submit"]))
 {
     #retrieve file title
        $title = $_POST["title"];
     
    #file name with a random number so that similar dont get replaced
     $pname = rand(1000,10000)."-".$_FILES["file"]["name"];
 
    #temporary file name to store file
    $tname = $_FILES["file"]["tmp_name"];
   
     #upload directory path
$uploads_dir = 'images';
    #TO move the uploaded file to specific location
    move_uploaded_file($tname, $uploads_dir.'/'.$pname);
 
    #sql query to insert into database
    $sql = "INSERT into fileup(title,image) VALUES('$title','$pname')";
 
    if(mysqli_query($conn,$sql)){
 
    echo "File Sucessfully uploaded";
    }
    else{
        echo "Error";
    }
}
 
 
?>

which was taken from this video:
https://www.youtube.com/watch?v=3OUTgnaezNY

On the web it looks like this:
enter image description here

It is successfully put the files in my directory:
enter image description here

But, it does not succeed in uploading those file to phpMyAdmin.
Namely, I think I have a problem regarding this block of code:

 $sql = "INSERT into fileup(title,image) VALUES('$title','$pname')";

    if(mysqli_query($conn,$sql)){

    echo "File Sucessfully uploaded";
    }
    else{
        echo "Error";
    }

Thank you very much in advance for your help

Phil
  • 157,677
  • 23
  • 242
  • 245
niroo
  • 49
  • 1
  • 8
  • [tag:phpmyadmin] is a tool for managing MySQL databases. It doesn't seem relevant to your question at all – Phil Nov 22 '21 at 06:49
  • 1
    That video is pure garbage, I cannot believe people are still publishing such poor practices only 3 years ago. Find one that teaches you how to use [prepared statements](https://www.php.net/manual/mysqli.quickstart.prepared-statements.php) at the very minimum – Phil Nov 22 '21 at 06:52
  • I use [phpmyadmin] in order to manage my MySQL database. As a result, the problem might be something that I have not configured right in [phpmyadmin]. So this is an information worth mentioning while asking for help regarding my problem. – niroo Nov 22 '21 at 06:54
  • 1
    As was mentioned in the link that you've attached I enabled the error message and got: `Fatal error: Uncaught mysqli_sql_exception: Table 'my_database.fileup' doesn't exist in /var/www/html/pages/images/index.php:56 Stack trace: #0 /var/www/html/pages/images/index.php(56): mysqli_query() #1 {main} thrown in /var/www/html/pages/images/index.php on line 56 ` – niroo Nov 22 '21 at 07:04
  • Sounds like you now know what the problem is. Is there anything about that error message that is unclear? – Phil Nov 22 '21 at 07:07
  • Just a moment, I try to solve it by myself by reading about prepared statements as you've mentioned. Please don't close this thread just yet, in case I will have upcoming question regarding my try. Your help is great, thank you very much Phil – niroo Nov 22 '21 at 07:12
  • While they are a coding best-practice, prepared statements aren't going to solve a missing DB table. You would still need to actually create your `fileup` table in the `CodeFlix` database (or is it the `my_database` database, it's not clear) – Phil Nov 22 '21 at 07:14
  • I must ask, why do you need phpMyAdmin? as the data itself is saved on the server and only the path is saved on phpMyAdmin? what is the benefit of that? I can just go into my folder on the server, type `ls` and see all of my stored data there – niroo Nov 22 '21 at 08:12
  • 1
    Again, phpMyAdmin is a tool for managing MySQL databases which is what you're actually talking about. Why you would want or need a database depends entirely on what you want to do with your data and how you intend to access it. Far too broad a topic for here – Phil Nov 22 '21 at 12:12

0 Answers0