php - 从PHP页面上传的blob图像从二进制图像转换为数据库中的真实图像?

标签 php mysql image blob admin

问题是,当我将 blob 图像从 PHP 页面上传到产品表时,它似乎是二进制的,但如果手动执行此操作,则可以正常工作。

请查看代码下的链接图像以了解我的意思。

干杯!

 <?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>");

        }       


?>

这是我的数据库表中显示的内容:

http://www.ya-techno.com/up/uploads/1429703619491.jpg

最佳答案

在将图像添加到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."' />";
}

关于php - 从PHP页面上传的blob图像从二进制图像转换为数据库中的真实图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29816879/

相关文章:

php - jQuery/PHP 喜欢/不喜欢按钮

php - 如何从从 mysqli_fetch_array() 获取的数组转换的 javascript 数组中获取所有行数据

php - 通过查询或脚本在 mysql 表中插入 rownums

c# - 淡入/淡出图像的最佳方式

database - 在数据库中存储图像 - 是还是否?

php - Laravel Eloquent 用户与 friend 的关系

php - 访问关联 PHP 数组中的对象时性能大幅下降

php - MySQL:如何在两个主键必须匹配的情况下进行重复键更新?

mysql - 无法使用 "startx"启动我的树莓派

javascript - 将图像 jquery 重新加载/替换为 <ul> 中的列表项