【问题标题】:Image from database is not displaying on webpage来自数据库的图像未显示在网页上
【发布时间】:2016-10-23 13:24:35
【问题描述】:

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

这里是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>

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

谢谢

【问题讨论】:

  • 停止使用mysql-*
  • @nogad 希望它这么简单,但它是这项任务所必需的
  • @iCoders 解析错误:语法错误,意外 '$row' (T_VARIABLE),期待 ',' 或 ';'在第 56 行的 \main_menu.php 中
  • @Thomas.you are using wrong src in image

标签: php html css sql


【解决方案1】:

你用错了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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多