这是我在数据库中插入和显示图像的代码(blob数据类型),Insertion每次都成功但我无法显示任何图像。 而且我不知道每次都会出现什么问题。
<?php
$hostname = "mysql.hostinger.in";
$username = "u881956746_jk";
$password = "jay@95373";
$dbname = "u881956746_quote";
$conn = mysqli_connect($hostname , $username , $password , $dbname);
if(!$conn)
{
die("Connection Failed : " . mysqli_connect_error());
}
if(isset($_POST['submit']))
{
if(isset($_FILES['file']))
{
$count = count($_FILES['file']['name']);
for($i = 0 ; $i < $count ; $i++)
{
$image = mysqli_real_escape_string($conn , $_FILES['file']['tmp_name'][$i]);
$image = base64_encode($image);
$sql = "INSERT INTO `u881956746_quote`.`quotes` (`Image`) VALUES ('$image');";
if(mysqli_query($conn , $sql))
{
echo "Successfully Inserted";
}
else
{
echo "Not inserted Succesfully";
}
}
}
else
{
echo "Nothing to Submit";
}
}
if(isset($_POST['Show']))
{
$sql = "SELECT * FROM `quotes`";
$result = mysqli_query($conn , $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<img height = "300" width = "400" src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/>';
echo "<br />";
}
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<body>
<form method = "POST" action = "practice2.php" enctype = "multipart/form-data">
<input type = "file" name = "file[]" id = "image" multiple />
<br />
<br />
<input type = "submit" name = "Show" value = "Show" />
<br />
<br />
<input type = "submit" name = "submit" value = "Submit File" />
</form>
</body>
</html>
答案 0 :(得分:0)
从显示图片中移除base64_encode
,因为它在插入期间已转换为base64
if(isset($_POST['Show']))
{
$sql = "SELECT * FROM `quotes`";
$result = mysqli_query($conn , $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<img height = "300" width = "400" src="data:image/jpeg;base64,'.$row['Image'].'"/>';
echo "<br />";
}
}
}
答案 1 :(得分:0)
最后得到了输出:
<?php
$hostname = "mysql.hostinger.in";
$username = "u881956746_jk";
$password = "jay@95373";
$dbname = "u881956746_quote";
$conn = mysqli_connect($hostname , $username , $password , $dbname);
if(!$conn)
{
die("Connection Failed : " . mysqli_connect_error());
}
if(isset($_POST['submit']))
{
if(isset($_FILES['file']))
{
$count = count($_FILES['file']['name']);
for($i = 0 ; $i < $count ; $i++)
{
$image = addslashes(file_get_contents($_FILES['file']['tmp_name'][$i]));
$sql = "INSERT INTO `u881956746_quote`.`quotes` (`Image`) VALUES ('$image');";
if(mysqli_query($conn , $sql))
{
echo "Successfully Inserted";
}
else
{
echo "Not inserted Succesfully";
}
}
}
else
{
echo "Nothing to Submit";
}
}
if(isset($_POST['Show']))
{
$sql = "SELECT * FROM `quotes`";
$result = mysqli_query($conn , $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<img height = "60" width = "80" src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/>';
echo "<br />";
}
}
}
mysqli_close($conn);
?>