Using PHP to upload file and add the path to MySQL database

后端 未结 2 904
北海茫月
北海茫月 2020-11-29 09:40

Upload.php:



        
相关标签:
2条回答
  • 2020-11-29 10:23
    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')" ;
    
    0 讨论(0)
  • 2020-11-29 10:24

    First you should use print_r($_FILES) to debug, and see what it contains. :

    your uploads.php would look like:

    //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=basename( $_FILES['Filename']['name']);
    $Description=$_POST['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['Filename']['name']). " has been uploaded, and your information has been added to the directory";
        // 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')") ;
    } else {
        //Gives and error if its not
        echo "Sorry, there was a problem uploading your file.";
    }
    
    
    
    ?>
    

    EDIT: Since this is old post, currently it is strongly recommended to use either mysqli or pdo instead mysql_ functions in php

    0 讨论(0)
提交回复
热议问题