0
我想將圖像上傳到我的數據庫,所以爲此我創建瞭如下表格。圖像(BLOB)不能完美地將圖像保存在數據庫中
而且使用下面的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轉換器轉換了相同的圖像,然後從我的數據庫代碼中獲得了不同的代碼。那麼最新的錯誤...?
Your'e用'addslashes()'和'mysqli_real_escape_string()'雙重轉義。只要使用準備好的聲明並停止這樣做。 – AbraCadaver
我也嘗試了一個,但沒有工作... –
插入的那些已經與雙重逃脫搞砸了,如果你在顯示它之前逃脫它將不會被糾正。 – AbraCadaver