我是php的新手,不幸的是,我尝试将数据插入到我的数据库中,但是我成功地插入了名称和价格,但是我陷入了插入图片(LONGBLOB)的困境,这是我的代码

<?php
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "tbl_product";
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if(isset($_POST['update']))
    {
        $name = $_POST['name'];
        $image = $_FILES['image'];
        $price = $_POST['price'];
        $statement = $conn->prepare('INSERT INTO tbl_product (name, image, price)
            VALUES (:name, :image, :price)');
        $statement->execute([
                ':name' => $name,
                ':image' => $image,
                ':price' => $price,
        ]);
    }
?>




<div class="settings-row">
    <h3>Name</h3>
    <form action="insertscript.php" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <input type="text" class="form-control" name="name">

            <h3>Image</h3>
            Select image to upload:
            <input type="file" name="image">

            <h3>Price</h3>
            <input type="number" class="form-control small-input" name="price" >
            <input type="submit" value="Submit" name="update" id="update">
         </div>
    </form>
</div>




<? echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'" class="img-responsive"/>';?><br />

最佳答案

根据我发表的评论,您似乎希望存储实际的文件数据而不是文件路径,因此在上传的图像上使用file_get_contents-也许像这样...

<?php

    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "tbl_product";

    $conn = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password );
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


    /*
        catch errors
    */
    try{
        /*
            test that imprtant variables are set
        */
        if( isset( $_POST['update'], $_POST['name'], $_POST['price'] ) && !empty( $_FILES['image'] ) ) {

            $name = $_POST['name'];
            $price = $_POST['price'];

            /*
                get reference to uploaded image
            */
            $obj=(object)$_FILES['image'];
            $tmp=$obj->tmp_name;
            $error=$obj->error;

            /*
                if there were no errors with upload, proceed to insert into db
            */
            if( $error == UPLOAD_ERR_OK && is_uploaded_file( $tmp ) ){

                /*
                    as the column is a longblob it suggests that you wish to store the actual file rather than the path
                    - this will lead to a mahoosive database in quite a short time!!
                */
                $image=base64_encode( file_get_contents( $tmp ) );


                $statement = $conn->prepare('INSERT INTO tbl_product (`name`, `image`, `price`) VALUES (:name, :image, :price)');
                $args=array(
                    ':name'     => $name,
                    ':image'    => $image,
                    ':price'    => $price
                );

                $result = $statement->execute( $args );

            } else {
                throw new Exception('Upload failed');
            }
        }
    }catch( Exception $e ){
        exit( $e->getMessage() );
    }
?>


要显示如上所述保存为base64编码数据的图像,需要修改img标记的语法。例如:

<img src='data:image/jpeg;base64, /9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPU...... etc etc

关于php - PHP错误,无法将图像插入数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54050653/

10-11 02:20
查看更多