2016-05-06 83 views
0

我想將圖像上傳到我的數據庫,所以爲此我創建瞭如下表格。圖像(BLOB)不能完美地將圖像保存在數據庫中

enter image description here

而且使用下面的PHP代碼上傳並顯示在同一頁上的圖像。

<?php 
// Connection To Database 
$host = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "my_db"; 
$connection = mysqli_connect($host,$username,$password,$dbname); 
if (!$connection) 
{ 
die('Could Not Connect To Database: ' . mysqli_connect_error()); 
} 
mysqli_query($connection, "SET NAMES utf8"); 

// Garb User Account Details 
$garb = mysqli_query($connection, "SELECT * FROM my_table"); 
if(!$garb){ 
$error = "<div class='error'>There's Little Problem: ".mysql_error()."</div>"; 
} else { 
//Showing The User Data 
while($row = mysqli_fetch_array($garb)) { 
    $A_Id = $row['A_Id']; 
    $A_ProfilePic = $row['A_ProfilePic']; 
} 


// Getting Image 
$A_ProfilePic = addslashes(file_get_contents($_FILES['A_ProfilePic']['tmp_name'])); //SQL Injection defence! 
$A_ProfilePic = mysqli_real_escape_string($connection, $A_ProfilePic); 


//Everything Is Okay So Let's Register This User 
$update = mysqli_query($connection, "UPDATE accounts SET A_ProfilePic='$A_ProfilePic' WHERE A_Id='999999'"); 
if(!$update){ 
$updateerror = "<div class='error'>There's Little Problem: ".mysql_error()."</div>"; 
} else { 
//Confirming Message To User 
$error = "<div class='success'>Your Pic Is Updated.</div><br/>"; 
} 
?> 

/* Getting Image */ 
<?php echo '<img class="profileAvatar" alt="" title="" src="data:image/jpeg;base64,'.base64_encode($A_ProfilePic).'"/>';?> 
<form action="" method="post" enctype="multipart/form-data"> 
<input type="file" name="A_ProfilePic" id="A_ProfilePic"/> 
<input type="submit" name="submit" value="Update Your Setting"></input> 
<input type="reset" name="reset" value="Reset Form"></input> 
</form> 

但我得到的錯誤作爲插入圖像,然後,當我的Base64編碼不完全插入,儘管檢索圖像爲什麼我不能夠看到它。我從在線Base64轉換器轉換了相同的圖像,然後從我的數據庫代碼中獲得了不同的代碼。那麼最新的錯誤...?

+1

Your'e用'addslashes()'和'mysqli_real_escape_string()'雙重轉義。只要使用準備好的聲明並停止這樣做。 – AbraCadaver

+0

我也嘗試了一個,但沒有工作... –

+0

插入的那些已經與雙重逃脫搞砸了,如果你在顯示它之前逃脫它將不會被糾正。 – AbraCadaver

回答

0

最後,我通過使用與上面幾乎相同但有一些變化的代碼得到它的工作。現在,通過此代碼,我可以在單個頁面上運行時更改和查看圖像。

<?php 
// Connection To Database 
$host = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "my_db"; 
$connection = mysqli_connect($host,$username,$password,$dbname); 
if (!$connection) 
{ 
die('Could Not Connect To Database: ' . mysqli_connect_error()); 
} 
mysqli_query($connection, "SET NAMES utf8"); 

// Garb User Account Details 
$garb = mysqli_query($connection, "SELECT * FROM my_table"); 
if(!$garb){ 
$error = "<div class='error'>There's Little Problem: ".mysql_error()."</div>"; 
} else { 
//Showing The User Data 
while($row = mysqli_fetch_array($garb)) { 
    $A_Id = $row['A_Id']; 
    $A_ProfilePic = $row['A_ProfilePic']; 
} 

// Update Image In Database 
if (isset($_POST['A_ProfilePic'])){ 
// Getting Image 
$A_ProfilePic_Add = mysqli_real_escape_string($connection, $A_ProfilePic); 
//Everything Is Okay So Let's Register This User 
$update = mysqli_query($connection, "UPDATE accounts SET A_ProfilePic='$A_ProfilePic_Add' WHERE A_Id='999999'"); 
if(!$update){ 
$updateerror = "<div class='error'>There's Little Problem: ".mysql_error()."</div>"; 
} else { 
//Confirming Message To User 
$error = "<div class='success'>Your Pic Is Updated.</div><br/>"; 
} 
header("Refresh:0"); 
} 
?> 

/* Getting Image */ 
<?php echo '<img class="profileAvatar" alt="" title="" src="data:image/jpeg;base64,'.base64_encode($A_ProfilePic).'"/>';?> 
<form action="" method="post" enctype="multipart/form-data"> 
<input type="file" name="A_ProfilePic" id="A_ProfilePic"/> 
<input type="submit" name="submit" value="Update Your Setting"></input> 
<input type="reset" name="reset" value="Reset Form"></input> 
</form>