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:
It is successfully put the files in my directory:
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