使用PHP从SQLite数据库显示BLOB数据

问题描述 投票:0回答:1

希望大家有一个伟大的一天,我与新DB SQLITE所以我有点困惑的处理BLOB data.I试过很多东西,但没有得到有利于我的。

这里是我的代码

插入数据

insert.php

<form method="POST" enctype="multipart/form-data">
        model no:<input type="text" name="model_no"\><span style="color: red"><?php echo $messages["model_no"]; ?></span>
        image:<input type="file" name="image" id="image"\><span style="color: red">
        <input type="submit" value="save" id="save" name="save"/>
        <input type="reset" value="Clear"/>
    </form>

include.php

<?php
    $flag = 0;
    class MyDB extends SQLite3
    {
        function __construct()
        {
            $this->open('../database.db');
        }
    }

    //checking save button is clicked
    if(isset($_POST["save"])){
        $model_no = $_POST['model_no'];
        $image = $_FILES['image']['name'];

        if($model_no != '' && $image != ''){
            $flag = 1;
        }

        if($flag == '1'){
            $db = new MyDB();

            if(!$db){
                echo $db->lastErrorMsg();
            } 
            else{
                    $dbb = new MyDB();

                    $sql = 'SELECT COUNT(*) as count FROM fisfis WHERE model_no =  "'.$model_no.'"';

                    //echo $sql;

                    $rows = $dbb->query($sql);
                    $row = $rows->fetchArray();
                    $numRows = $row['count'];
                    //echo $numRows;

                    if($numRows != 0){
                        $flag = 0;
                        echo "error:this model no is already taken";
                    }else{
                        $sqlp = "INSERT INTO fisfis(model_no, image) values('$model_no','$image')";

                        if($dbb->exec($sqlp)){
                            echo "data inserted successfully\n";
                        }
                        else{
                            echo"error:\n";
                            echo "data not inserted \n";
                            echo"contact admin for details\n";
                        }
                    }

           }
        }
        else{
                        echo"error:\n";
                        echo "data not inserted : data fields cannot be empty\n";
            }

    }   
?>

这是非常成功插入数据到我的表(表名:fisfis)

显示数据

Display.php的

<form method="POST" enctype="multipart/form-data">
        <p>Enter Barcode : <input type="text" name="search_model" id="search_model" /></p> 
        <!--<p><img src='image.php?id=<?php //echo $row['model_no'];?>'/></p>-->
        <p><input type="submit" value="search" name="search" id="search"/></p>
    </form>

display_include.php

<?php

    $flag2 = 0;
    $flag3 = 0;

    class MyDB extends SQLite3
    {
        function __construct()
        {
            $this->open('../database.db');
        }
    }

    $db = new MyDB();

    if(isset($_POST["search"])){
        $model_no = '';
        $model_no = $_POST['search_model'];

        if($model_no != ''){
            $flag2 = 1;
        }

        if($flag2 == '1'){
            $db = new MyDB();

            if(!$db){
                echo $db->lastErrorMsg();
            } 

            else{
                $dbb = new MyDB();

                $sql = 'SELECT COUNT(*) as count FROM fisfis WHERE model_no =  "'.$model_no.'"';

                $rows = $dbb->query($sql);
                $row = $rows->fetchArray();
                $numRows = $row['count'];
                echo $numRows;

                if($numRows == 0){
                    echo 'sorry this model no is not exists';
                    echo '<br/>';
                    echo '<a href="insert_controller.php">create new</a>';
                    $flag2 = 1;
                }else{
                    echo "this is exisists";
                    $flag3 = 1;
                }

                if($flag3 == 1){
                    $sqla = 'SELECT * FROM fisfis WHERE model_no =  "'.$model_no.'"';
                    $result = $dbb->query($sqla);

                    while($row = $result->fetchArray(SQLITE3_ASSOC) ) {

                        echo "MODEL NO = ". $row['model_no'] ."\n";

                        $img = '\'<img src="'. $row['image'] .'" width="100" height="100"/>\'';
                        echo $img;

                   }

                }

            }

        }
    }


?>

显示部分显示比其他的blob的所有数据,我曾尝试

1.try呼应PHP脚本内的图像

$img = '\'<img src="'. $row['image'] .'" width="100" height="100"/>\'';
  1. 试图显示HTML标签内的图像

但是这不工作,请大家帮我解决这个问题,在此先感谢

php image sqlite blob
1个回答
0
投票

我的解释是不好的,所以这里是一个展示和告知。我做了一个摄制,所以我不会忘记任何必要位。

该数据库表的定义:

CREATE TABLE `imgrepro` (
        `id`    INTEGER NOT NULL,
        `photo` BLOB,
        PRIMARY KEY(`id`)
);

它种植有一排,ID = 1,照片为NULL。

POST块将插入图像插入在db BLOB列。脚本的其余部分将获取的图像和HTML将显示它。

<?php

    $db = new SQLite3("***********\stacktest.db"); 

    if ($_SERVER['REQUEST_METHOD'] == "POST") {
        // insert photo
        $photo = file_get_contents($_FILES['fname']['tmp_name']);
        $query = $db->prepare("UPDATE imgrepro set photo = :photo where id = 1");
        $query->bindValue(':photo',$photo,SQLITE3_BLOB);
        $result = $query->execute();
    }
    // get photo if there is one

    $rows = $db->query("SELECT * from imgrepro")->fetchArray(SQLITE3_ASSOC);


    $showphoto="";
    if (count($rows) > 0) {
        $showphoto=base64_encode($rows['photo']);
    }
    $db->close();

?>
<!DOCTYPE html>
<head>
<title>Say Cheese!</title>
</head>
<!-- show the photo -->
<img  alt="no photo yet" src="data:image/jpeg;base64,<?= $showphoto ?>" >
<form action="imgrepro.php" method="post" enctype="multipart/form-data">
<input type="file" name="fname" accept=".jpg">
<input type="submit">
</form>
© www.soinside.com 2019 - 2024. All rights reserved.