希望大家都度过了美好的一天,我是SQLITE
DB的新手,所以我对处理BLOB数据不感到困惑。我尝试了很多事情,但没有任何事情对我有利。
这是我的代码
插入数据
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。尝试回显php脚本中的图像
$img = '\'<img src="'. $row['image'] .'" width="100" height="100"/>\'';
尝试在html标签中显示图像
但是这不起作用,请先帮助我解决此问题,
答案 0 :(得分:0)
我的解释不好,所以这里有个表演。我做了一个再现,所以我不会忘记任何必要的地方。
数据库表已定义:
CREATE TABLE `imgrepro` (
`id` INTEGER NOT NULL,
`photo` BLOB,
PRIMARY KEY(`id`)
);
它的种子是一行,id = 1,照片为NULL。
POST
块会将图像插入数据库的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>