2016-10-23 12 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(); 
} 

?> 

は、(iを表示する画像を希望)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
あなたは次のコード

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

代わりに上記のコード使用の間違ったPHPのタグを使用している

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

    </td> 
関連する問題