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

oaxa6hgo  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(283)

我是新来的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 />
qyzbxkaa

qyzbxkaa1#

根据我的评论,您似乎希望存储实际的文件数据,而不是使用的文件路径 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
aydmsdu9

aydmsdu92#

我认为你的数据库和你的表不一样。。尝试此操作并替换'----datatable---'。您必须将图像转换为包含文件内容的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
  ]);
  }
}

?>

相关问题