PHP错误,无法将图像插入数据库

时间:2019-01-05 09:22:05

标签: php html mysql database pdo

我是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 />

2 个答案:

答案 0 :(得分:0)

我认为您的数据库与表不一样。尝试此并替换'---- datatable ---'。 并且必须使用file_get_contents将图像转换为blob

<?php
if (isset($_POST)) {
  $servername = "localhost";
  $username = "root";
  $password = "root";
  $dbname = "----datatable---";
      $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 = file_get_contents($_FILES['image']['tmp_name']);
  $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
  ]);
  }
}

?>

答案 1 :(得分:0)

根据我的评论,看来您希望存储实际的文件数据而不是文件路径,因此在上载的图像上使用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