使用INSERT INTO上传数据库信息

时间:2015-08-12 16:30:15

标签: php database mysqli insert-into

这是我的图片上传代码,它工作正常,但我在使用函数“INSERT INTO”将路径放入我的数据库时遇到问题,它不起作用。 我的图像转到文件夹,但我的数据库中表'pages'的列'Image'是空白的,没有上传路径。 有什么建议吗?

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_localhost = "localhost";
$database_localhost = "database";
$username_localhost = "root";
$password_localhost = "";
$localhost = new mysqli($hostname_localhost, $username_localhost, $password_localhost);

if ($localhost->connect_error) {
  die('Connect Error (' . $localhost->connect_errno . ') '. $localhost->connect_error);
} 
?>
<?php
if(isset($_FILES['UploadFileField'])){

    // Creates the Variables needed to upload the file

    $UploadName = $_FILES['UploadFileField']['name'];
    $UploadName = mt_rand(100000, 999999).$UploadName;
    $UploadTmp = $_FILES['UploadFileField']['tmp_name'];
    $UploadType = $_FILES['UploadFileField']['type'];
    $FileSize = $_FILES['UploadFileField']['size'];

    // Removes Unwanted Spaces and characters from the files names of the files being uploaded

    $UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);

    // Upload File Size Limit 

    if(($FileSize > 250000)){

        die("Error - File to Big");

    }

    // Checks a File has been Selected and Uploads them into a Directory on your Server

    if(!$UploadTmp){
        die("No File Selected, Please Upload Again");
    }else{
        $move=move_uploaded_file($UploadTmp, 'images/'.$UploadName);

        mysqli_query($localhost,"INSERT INTO pages (Image) 
VALUES (/pages/images/.$UploadName)");

mysqli_close($localhost);

    }
    if($move) {

   echo("file ".$UploadName." has been uploaded!");
   echo("<br>/pages/images/".$UploadName);
    }else {
        exit("unable to upload the file" .$UploadName);
        }
        }

?>

谢谢!

0 个答案:

没有答案