2013-04-09 117 views
0

我正在做一個上傳和請求標識以在上載後檢索圖像。但是,在我可以檢索之前,我甚至可以將它插入到我的MySQL數據庫中;無法在MySQL中插入圖像

BTW問題是這是

"$query = "INSERT INTO profilepicture (`id`,`name`,`image`) VALUES ('','".$image_name."','".$image."')";" 

這行19是我的代碼,我用

<?php 
$con = mysqli_connect('127.0.0.1', 'root', '', 'test'); 
$files = $_FILES['uploadProfilePicture']['tmp_name']; 

if(!isset($files)){ 
echo("wrong file"); 
}else 
{ 
$image = file_get_contents($_FILES['uploadProfilePicture']['tmp_name']); 
$image_name = $_FILES['uploadProfilePicture']['name']; 
$image_size = getimagesize($_FILES['uploadProfilePicture']['tmp_name']); 

if($image_size == false) 
{ 
    echo("Thats is not an image"); 
}else 
{ 
    $query = "INSERT INTO profilepicture (`id`,`name`,`image`) VALUES ('','".$image_name."','".$image."')"; 
    if(!$insert = mysqli_query($con, $query)) 
    { 
    echo("problem uploading"); 
    }else 
    { 

     $lastid = mysql_insert_id(); 
     echo "image uploaded.</p> Your Image</p> <img src=getImage.php?id=".$lastid.">"; 
    } 
} 
} 
?> 



<div class="loginCheck"> 
     <div class="profilePicture"> 
      <form action="ProfileImages/FileUpload.php" method="POST" enctype="multipart/form-data"> 
       <input type="file" name="uploadProfilePicture"/> <input type="submit" value="Upload" /> 
      </form> 



     </div> 
    </div> 
+0

你會得到什麼錯誤? – jtheman 2013-04-09 22:01:57

回答

0

我敢打賭,你有這一行的一個問題:

$image = file_get_contents($_FILES['uploadProfilePicture']['tmp_name']); 

試試這個:

$image = mysql_real_escape_string(file_get_contents($_FILES['uploadProfilePicture']['tmp_name'])); 

您需要轉義該字符串。它可能有一個報價。你應該看看使用prepared statements

+0

更好,只需base64_encode圖像數據 – Ozzy 2013-04-09 22:12:14

0

這個字符串應該如何構造?

"$query = "INSERT INTO profilepicture (`id`,`name`,`image`) VALUES ('','".$image_name."','".$image."')";" 

如上所述?或:

$query = "INSERT INTO profilepicture (`id`,`name`,`image`) VALUES ('','".$image_name."','".$image."')"; 

通知的$前和結束;去除報價?