来自数据库的图片未显示在网页上

时间:2016-10-23 04:27:32

标签: php html css sql

所以我创建了这个博客,但是我遇到了将数据库中的图像显示到页面本身的问题。它只会出现破碎的图像。但数据确实出现在数据库中。它只是不显示在页面上。

这是image.php代码(用于显示文本和图像):

<html>
<body>
<?php
//connect to database

//Name the variables
$host= "localhost";
//Localhost is the name of the computer that USBWebser has been loaded on
$username = "user";
$password = "pwd";
$database = "blog";

$dbh=mysql_connect("$host", "$username", "$password") or die('Could not connect:' .mysql_error());

//if cannot connect to database display error message
if(!$dbh)
{
echo mysql_error();
}

mysql_select_db("$database");

//get the id number of the row that the photo is located in and place it in $ano
$ano=$_GET['postID'];

//select the data and type for the photo identified by id
$sql="SELECT photo, phototype FROM blog where postID='$ano'";

//check if sql query can be executed
$r=mysql_query($sql, $dbh);

//if sql query can be executed
if($r)
{

//get the data from the query
$row=mysql_fetch_array($r);

//set the header information so that an image can be displayed
$type="Content-type: image/png" .$row['phototype'];
header($type);

//display the image
echo $row['photo'];
}
else
{
echo mysql_error();
}

?>

这是main_menu.php的代码(我希望图片出现在哪里)

<?PHP
//Name the variables
$host= "localhost";
//Localhost is the name of the computer that USBWebser has been loaded on
$username = "user";
$password = "pwd";
$database = "blog";

$mysqli=new mysqli($host, $username, $password, $database);

//Connect to Header
include "header.php";
?>
<?php
//Select fields from the posts table
$sql="SELECT postID, title, date, contents, rating, photo, phototype FROM posts";
//Place the data into a variable named $result
$result= $mysqli->query($sql);

if ($result->num_rows>0){

while ($row=$result->fetch_assoc()) {
?>
<br><table border="1" bordercolor="25dae3" width="53%"><th><font color="white">Title</th><th><font color="white">Date</th><th><font color="white">Contents</th><th><font color="white">Image</th><th><font color="white">Rating</th>
<tr><td width = "100" align="center"><font color="white">
<?php 
echo $row["title"];
?>
</td>
<br><td width="100" align="center"><font color="white">
<?php
echo $row["date"];
?>
</td>
<br><td width="300" align="center"><font color="white">
<?php
echo $row["contents"];
?>
</td>
<br><td width="300" align="center"><font color="white">
<img src="<?php echo $row['photo']; ?>" width=300 height=300/>;
</td>
<br><td width="100" align="center"><font color="white">
<?php
echo $row["rating"];
?>
</td></tr></font>
<?php
}
} else {
    //Display message that no data was present
    echo "0 results";
}
//Close connection
$mysqli->close();
?>

add_post.php

<html>
<header>
</header>
<body>
<?php
//Name the variables
$host = "localhost";
//Localhost is the name of the computer that USBWebserver has been loaded on 
$username = "user";
$password = "pwd";
$database = "blog";

$mysqli=new mysqli($host, $username, $password, $database);

//Get variables from the form 
$new_post_title=$_POST["newtitle"];
$new_post_date=$_POST["newdate"];
$new_post_contents=$_POST["newcontents"];
$new_post_rating=$_POST["newrating"];
$photo=addslashes(file_get_contents($_FILES["photo"]["tmp_name"]));
$imagesize=getimagesize($_FILES["photo"]["tmp_name"]); 

//mime returns the image time eg. image/jpeg
$imagetype=$imagesize['mime'];

//Enable sql to read quotation marks within sentences
$new_post_title=addslashes($new_post_title);
$new_post_date=addslashes($new_post_date);
$new_post_contents=addslashes($new_post_contents);
$new_post_rating=addslashes($new_post_rating);

//Enter the new information into the posts table 
$sql="INSERT INTO posts(postID, title, date, contents, rating, photo, phototype) VALUES (Null, '$new_post_title', '$new_post_date', '$new_post_contents', '$new_post_rating', '$photo', '$imagetype')"; 

//Run the query 
$result=$mysqli->query($sql) or die (mysqli_error($mysqli)); 

if ($result) {

header ('location:main_menu.php');
}

else {
echo mysql_error();
}
?> 
</body>
</html>

以及向博客提交帖子的表格(add_new_post.php)

<HTML>
<style>
form {
    border-opacity: 1.0 ;
    display: incline-block;
    text-align: center;
}

input[type=text]:focus, input[type=date]:focus {
width: 50%;
height: 20%;     
border: 3px solid #00ffff;   
}

body {
   text-align: center;
   padding-top: 50px; 
 }

</style>
<HEAD>
</HEAD>
<BODY><font color="white">
<br><br><br><H1 text-align="center">Add a New Post</H1>
<?php 
//Connect To Header Page
include "header1.php";

//Connect To Database
include "dbconnect.php";
?>
<br>
<br>
<!-- <HR> Tag inserts a horizonal line across the page (horizontal rule)-->
<!-- <Form> Tag indicates that a form will be created -->
<!-- action indicates the file used to process the input when the submit button is pressed-->
<form enctype= "multipart/form-data" action="add_post.php" method = "POST">
Title: <br>
<!-- <input type> Tag indicates the type of input expected eg. text. Name = indicates the name given to the input-->
<input type="text" name="newtitle"><br>
Date: <br>
<input type="date" name="newdate"><br>
Contents: <br>
<input type="text" name="newcontents"><br>
Rating: <br>
<input type="text" name="newrating"><br>
Please Browse to where the photo is located:<br> 
<input type = file name = "photo"><br>
<br>
<!-- Value indicates the text to be displayed. In this case, displayed on the button -->
<input type ="submit" value="Submit">
</form>
</BODY>
</HTML>

对此问题的任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

你使用的是php标签错误

<br><td width="300" align="center"><font color="white">
<?php
echo "<img src="<?php echo $row['photo']; ?>" width=300 height=300/>";  
?>
</td>

而不是上面的代码使用以下代码

 <br><td width="300" align="center"><font color="white">
   <img src="<?php echo $row['photo']; ?>" width=300 height=300/>

    </td>