2016-10-23 98 views
-2

所以我有這個博客,我正在創建,但我有問題顯示圖像從數據庫到頁面本身。它只是出現了一個破碎的圖像。數據確實出現在數據庫中。它只是不顯示在頁面上。從數據庫中的圖像不顯示在網頁上

這裏是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> 

對此問題的任何幫助將不勝感激。

感謝

+0

停止使用mysql- * – nogad

+0

」?; ?> – iCoders

+0

@nogad希望它是如此簡單,但它是此任務所必需的 – Thomas

回答

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> 
相關問題