【问题标题】:How i upload blob image into my product table?我如何将 blob 图像上传到我的产品表中?
【发布时间】:2015-04-15 08:33:31
【问题描述】:

我尝试了很多方法让我选择的图像上传到我的产品表中,但没有发生任何错误这里是我的 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");


    //three pages one for form and anthor one for the search page.

            /*if (isset($_POST['sSearch']))
            {

                processForm();
        }*/


        if (isset($_POST['sAddProduct']))
        {

            addNewProduct();
        }

            else if(isset($_POST['submit'])){

                addNewImage();
            }

            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
            //hasn't yet been submitted, display the form
        {
            showForm();
        }

function addNewProduct()
{
        $ProductName = $_POST['Product_Name'];
        $ProductPrice = $_POST['Price'];


        //database query to add country
        $insertStringProduct = "INSERT into Product(Product_Name, Price)
        VALUE('$ProductName', '$ProductPrice')" ;
        $result = mysql_query($insertStringProduct);
        echo ("<p1>Product added Successfully</p1>");
        echo("<FORM><INPUT Type='button' VALUE='Back' onClick='history.go(-1);return true;'></FORM>");




}


///////>>>>>>>>>>>>>>Here what i added but nothing happened<<<<<<<<<<<<<<<<<<<<<<<
function addNewImage(){

    $image = $_POST['Image'];
    $sql = "INSERT INTO product
    (Image)
    VALUE
    ( '$image')" echo "Please Upload an image..";
    $result=mysql_query($sql) or die("error in uploading/*");

} 

这是我使用的表格:

这是我的数据库产品表,您可以在其中找到图像行 http://www.ya-techno.com/up/uploads/1429085907261.png

【问题讨论】:

    标签: php image blob


    【解决方案1】:

    你错了:

    $image = $_FILES['Image'] 而不是$_POST['Image']

    然后尝试以下操作:

    $image = mysql_escape_string(file_get_contents($image['tmp_name']));
    

    按照你已经在做的那样传递这个变量。 mysql_escape_string 会使其更加准确和精确。

    【讨论】:

    • 这是我的图片表单
    • 它应该是&lt;form&gt; 而不是&lt;from&gt;。另外,请确保您的表单也已关闭,例如:&lt;/form&gt;,因为我在您的评论中找不到它。
    【解决方案2】:

    请检查我是否更新了功能:

    function addNewImage(){
    
        $image = $image = addslashes(file_get_contents($_FILES['Image']['tmp_name']));
        $sql = "INSERT INTO product(Image) VALUES ('".$image."')";
        $result=mysql_query($sql) or die("error in uploading/*");
    
    }
    

    尝试像这样更改函数,您还需要存储文件扩展名。这将有助于从数据库中获取文件,并且您还需要使用 PDO。

    你的表格应该是这样的:

    <form action='form.php' method='POST' enctype='multipart/form-data'> <input type='file' name='Image'> <input type='submit' name='submit' value='Upload'>
    </from>
    

    【讨论】:

    • 不工作whats 'tmp_name'?我还没有把这个放到我的产品表中
    • 请尝试回显 $image 并发送您的表单代码。
    • 这是我的图片表单
    猜你喜欢
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2017-07-12
    • 2022-11-04
    • 1970-01-01
    • 2020-01-25
    相关资源
    最近更新 更多