Search code examples
phpmysqlimageblobadmin

How to convert the blob image from binary to real image in database when it uploaded from PHP page?


The problem is when I uploaded the blob image to the product table from PHP page it seems as binary, but if do that manually that's working.

Please see the link image under the code to what I mean.

Cheers!

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

        }       


?>

Here is what has shown in my database table:

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


Solution

  • You could try to use addslashes instead of mysql_real_escape_string before you add the image to db in addproduct and then when you query the db to do something like this:

    $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."' />";
    }