[php] Using PHP to upload file and add the path to MySQL database

Upload.php:

<?php

//This is the directory where images will be saved
$target = "pics";
$target = $target . basename( $_FILES['Filename']['name']);

//This gets all the other information from the form
$Filename=$_POST['Filename'];
$Description=$_POST['Description'];
$pic=($_FILES['Filename']['name']);


// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO picture (Filename,Description)
VALUES ('$Filename', '$Description')") ;

//Writes the Filename to the server
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target)) {
    //Tells you if its all ok
    echo "The file ". basename( $_FILES['uploadedfile']['Filename']). " has been uploaded, and your information has been added to the directory";
} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}
?>

And here is the form(in a separate file):

<form method="post" action="upload.php" enctype="multipart/form-data">
    <p>Photo:</p>
    <input type="file" name="Filename"> 
    <p>Description</p>
    <textarea rows="10" cols="35" name="Description"></textarea>
    <br/>
    <input TYPE="submit" name="upload" value="Add"/>
</form>

The errors are

 Undefined index: Filename on Line 17

(the $Filename=$_POST['Filename'];)

and

Undefined index: uploadedfile on Line 35

(the echo "The file ". basename( $_FILES['uploadedfile']['Filename']). " has been uploaded, and your information has been added to the directory";)

echo"<pre>".print_r($_FILES,true)."</pre>";

gives me:

Array
(
    [Filename] => Array
        (
            [name] => Laserkanon.jpg
            [type] => image/jpeg
            [tmp_name] => C:\WampServer\tmp\php11D4.tmp
            [error] => 0
            [size] => 41813
        )

)

This question is related to php html mysql web mysql-loadfile

The answer is


mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("altabotanikk") or die(mysql_error()) ;

These are deprecated use the following..

 // Connects to your Database
            $link = mysqli_connect("localhost", "root", "", "");

and to insert data use the following

 $sql = "INSERT INTO  Table-Name (Column-Name)
VALUES ('$filename')" ;

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to web

Cannot open local file - Chrome: Not allowed to load local resource How do I detect if a user is already logged in Firebase? HTML button opening link in new tab What does "app.run(host='0.0.0.0') " mean in Flask Web link to specific whatsapp contact App not setup: This app is still in development mode How to delete/remove nodes on Firebase Cannot read property 'push' of undefined when combining arrays HTML: Image won't display? JWT (JSON Web Token) library for Java

Examples related to mysql-loadfile

Using PHP to upload file and add the path to MySQL database How to insert image in mysql database(table)? MYSQL import data from csv using LOAD DATA INFILE MySQL: Enable LOAD DATA LOCAL INFILE How to import XML file into MySQL database table using XML_LOAD(); function How to insert selected columns from a CSV file to a MySQL database using LOAD DATA INFILE