[英]How to convert the blob image from binary to real image in database when it uploaded from PHP page?
問題是,當我從PHP頁面將blob圖像上傳到產品表時,它看起來像是二進制文件,但是如果手動執行該操作就可以了。
請查看代碼下方的鏈接圖片,以了解我的意思。
干杯!
<?php
//connect to the server and create database.
$host = "localhost";
$userMS = "";
$passwordMS = "";
$connection = mysql_connect($host,$userMS,$passwordMS) or die("Couldn't connect:".mysql_error());
$database = "projectDataBase";
$db = mysql_select_db($database,$connection) or die("Couldn't select database");
if (isset($_POST['sAddProduct']))
{
addNewProduct();
}
else if(isset($_POST['delete']))
{
$Product_ID=$_POST['Product_ID'];
$mysqlquery = "delete from Product where Product_ID= ".$Product_ID."";
mysql_query($mysqlquery);
echo "Deleted successfully";
echo("<FORM><INPUT Type='button' VALUE='Back' onClick='history.go(-1);return true;'></FORM>");
}
else
{
showForm();
}
// add new product
function addNewProduct()
{
$ProductName = $_POST['Product_Name'];
$ProductPrice = $_POST['Price'];
$Gender = $_POST['Gender_ID'];
$Category = $_POST['Category_ID'];
$Status = $_POST['Status_ID'];
$Age = $_POST['Age_ID'];
$image = $_FILES['Image'];
$image = mysql_real_escape_string(file_get_contents($image['tmp_name']));
//database query to add product
$insertStringProduct = "INSERT into Product(Product_Name, Price,Gender_ID, Category_ID,Status_ID,Age_ID,Image)
VALUE('$ProductName', '$ProductPrice', '$Gender', '$Category', '$Status', '$Age',''".$image."'')";
$result = mysql_query($insertStringProduct);
echo ("<p1>Product added Successfully</p1>");
echo("<FORM><INPUT Type='button' VALUE='Back' onClick='history.go(-1);return true;'></FORM>");
}
//function for the form page
function showForm()
{
//First form for adding new product
$self = htmlentities($_SERVER['PHP_SELF']);
echo("<form action = '$self' method='POST'>
<fieldset>
<legend>Adding New Product</legend>
Product Name: <input name='Product_Name' type='text' size = '40'>
<br /><br />
Price: <input name='Price' type='text' size = '20'><br><br />
Gender:
<select name='Gender_ID'>
<option value = '%'> <-- select--></option>");
//database query to show the country in the options from the database "product" field.
$dbQuary = " SELECT DISTINCT Gender_ID, Gender_Description from Gender";
$result = mysql_query($dbQuary);
while($row = mysql_fetch_row($result)){
echo("<option value ='$row[0]'> $row[1]</option>");
}
echo("
</select> <br/><br/>
Category:
<select name='Category_ID'>
<option value = '%'> <-- select--></option>");
//database query to show the country in the options from the database "product" field.
$dbQuary = " SELECT DISTINCT Category_ID, Description from Category";
$result = mysql_query($dbQuary);
while($row = mysql_fetch_row($result)){
echo("<option value ='$row[0]'> $row[1]</option>");
}
echo("
</select><br/><br/>
Status:
<select name='Status_ID'>
<option value = '%'> <-- select--></option>");
//database query to show the country in the options from the database "product" field.
$dbQuary = " SELECT DISTINCT Status_ID, Availability from Status";
$result = mysql_query($dbQuary);
while($row = mysql_fetch_row($result)){
echo("<option value ='$row[0]'> $row[1]</option>");
}
echo("
</select><br/><br/>
Age:
<select name='Age_ID'>
<option value = '%'> <-- select--></option>");
//database query to show the country in the options from the database "product" field.
$dbQuary = " SELECT DISTINCT Age_ID, Age_Description from Age";
$result = mysql_query($dbQuary);
while($row = mysql_fetch_row($result)){
echo("<option value ='$row[0]'> $row[1]</option>");
}
echo("
</select><br/><br/>
<form action='form.php' method='POST' enctype='multipart/form-data'> <input type='file' name='Image'> <input type='submit' name='sAddProduct' value='Upload'>
</fieldset>
</form>");
}
?>
這是我的數據庫表中顯示的內容:
在將圖像添加到addproduct中的db之前,然后查詢數據庫時,您可以嘗試使用addlashes而不是mysql_real_escape_string:
$sql = "SELECT Image FROM Product WHERE ProductId='.$product_id.'";
$result = mysqli_query($db,$sql);
while($imgarr= mysqli_fetch_array($result))
{
echo "<img src='php/showimage.php?ProductId=".$imgarr."' />";
}
我將從實際上傳文件開始。 將此添加到您的表單: enctype='multipart/form-data'
。 沒有它,您將永遠不會上傳任何東西。
做一些驗證。 確保您確實上傳了一些東西。
了解有關MVC模式和OOP的信息。 這將使您(和您的同事)的生活更加輕松。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.