当从 PHP 页面上传时,如何将 blob 图像从二进制图像转换为数据库中的真实图像?
How to convert the blob image from binary to real image in database when it uploaded from PHP page?
问题是当我从 PHP 页面将 blob 图像上传到产品 table 时,它看起来是二进制的,但如果手动这样做,那是可行的。
请看代码下的link图片了解我的意思。
干杯!
<?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>");
}
?>
这是我的数据库中显示的内容 table:
在将图像添加到 addproduct 中的数据库之前,您可以尝试使用 addslashes 而不是 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。这将使您(和您的同事)的生活更轻松。
问题是当我从 PHP 页面将 blob 图像上传到产品 table 时,它看起来是二进制的,但如果手动这样做,那是可行的。
请看代码下的link图片了解我的意思。
干杯!
<?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>");
}
?>
这是我的数据库中显示的内容 table:
在将图像添加到 addproduct 中的数据库之前,您可以尝试使用 addslashes 而不是 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。这将使您(和您的同事)的生活更轻松。