如何通过上传将文件名存储到mysql

时间:2016-01-31 04:31:51

标签: php mysql

这个mycode 我尝试编辑但没有成功。

if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"docfile/".$_FILES["filUpload"]["name"]))
    {
        //*** Insert Record ***//
        $file = $_FILES["filUpload"]["name"];
        $fileup = mysql_real_escape_string($file);
        $strSQL = "INSERT INTO fileproject(filename)  VALUES($fileup);";
        if(!$strSQL){
            echo "<h1>Error Store FileName2DB<h1>";
            exit;
        }
        echo "Upload Complete<br>";
    }

2 个答案:

答案 0 :(得分:3)

你不执行你的查询。您必须将对象用于数据库以进行查询。

  try
 {
    $bdd = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 'root', 'yourpassword',
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
 }
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"docfile/".$_FILES["filUpload"]["name"]))
{
    //*** Insert Record ***//
    $file = $_FILES["filUpload"]["name"];
    $fileup = mysql_real_escape_string($file);
    $strSQL = "INSERT INTO fileproject(filename)  VALUES($fileup)";
    $result=$bdd->query($strSQL);
    if(!$strSQL){
        echo "<h1>Error Store FileName2DB<h1>";
        exit;
    }

    echo "Upload Complete<br>";
}

答案 1 :(得分:0)

您没有使用mysql扩展来执行查询。其他示例即将PDO

您还可以使用INSERT扩展程序mysqli_*新行。

MYSQL程序结构:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

 if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"docfile/".$_FILES["filUpload"]["name"]))
 {
     //*** Insert Record ***//
     $file = $_FILES["filUpload"]["name"];
     $fileup = mysql_real_escape_string($file);
     $strSQL = "INSERT INTO fileproject (filename)  VALUES ('$fileup')";


     // here you need to use mysqli_* extension
     if(mysqli_query($conn, $strSQL)){
         echo  "Upload Complete<br>";
     }
     else{
         echo "<h1>Error Store FileName2DB<h1>";
         // exit;
     }
 }


mysqli_close($conn); // DATABASE CONNECTION CLOSE
?>

旁注:

正如我在评论$fileup中提到的,value是一个字符串值,因此您需要在变量之间使用单引号。