PHP MySQL从数据库中显示图像

时间:2014-02-15 14:39:19

标签: php html mysql

您好我有形式用信息和图像制作新文章。我怀疑将所有信息和图像保存到数据库(我猜)。当我想显示信息和图像时,只有信息有效。 见图。

有任何帮助吗?非常感谢

Picture

Inserting_post.php

<!DOCTYPE html>
    <html lang="en">
 <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>APK Market</title>
<link rel="stylesheet" type="text/css" href="../style/style.css">
<link href="../bootstrap/css/bootstrap.css" rel="stylesheet" media="screen">
<script src="../bootstrap/js/bootstrap.min.js"></script>
<script src="../bootstrap/js/bootstrap.js"></script>
 </head>
 <body>

 <form method="post" action="insert_post.php" enctype="multipart/form-data">

<div class="new_post">
<div class="headtitle">Insert new post</div>
 <div class="new_title">
<p>New title</p>
<input type="text" name="title">
 </div>
 <div class="new_author">
<p>Author</p>
<input type="text" name="author">
 </div>
 <div class="new_keywords">
<p>Keywords</p>
<input type="text" name="keywords">
   </div>
   <div class="new_image">
<p>Image</p>
<input type="file" name="image">
  </div>
  <div class="new_content">
<textarea name="content" cols="20" rows="8"></textarea>
   </div>
  <div class="submit">
<input type="submit" name="submit" value="OK">
  </div>
  </div>
 </form>
  <script src="https://code.jquery.com/jquery.js"></script>
  <script src="../bootstrap/js/bootstrap.js"></script>
  </body>
  </html>
   <?php
   include("../includes/connect.php");

   if(isset($_POST['submit']))
   {
$games_date = date('y-m-d-h');
$games_title = $_POST['title'];
$games_author = $_POST['author'];
$games_keywords = $_POST['keywords'];
$games_image = $_FILES['image']['name'];
$games_tmp = $_FILES['image']['tmp_name'];
$games_content = $_POST['content'];

if($games_title=="" or $games_author=="" or $games_keywords==""
     or $games_content=="")
{
    echo"<script>alert('any field is empty')</script>";
    exit();
}
else 
move_uploaded_file($games_tmp,"../uploaded_images/$games_image");
$insert_query= "insert into games(games_title,games_date,games_author,games_image,
    games_keywords,games_content)

      values ('$games_title','$games_date','$games_author','$games_image',
   '$games_keywords','$games_cont         ent')";

      }
         if(mysql_query($insert_query))
{
    echo "<center><h1>Post published seccesfuly!</h1></center>";
}
     ?>

     display page:

      <div class="content">
   <?php
     include('connect.php');

    $select_posts = "select * from games";
   $run_posts = mysql_query($select_posts);

   while($row=mysql_fetch_array($run_posts))
     {
echo '<p class="games_title_result">' .$games_title = $row['games_title'];
echo '<p class="games_image_result"><img src="<?php echo $row["games_image"];?>';
echo '<p class="games_content_result">' .$games_content = $row['games_content'];
echo '<p class="games_date_result">' .$games_date = $row['games_date'];
echo '<p class="games_author_result">' .$games_author = $row['games_author'];
     }
    ?>  
    </div>

3 个答案:

答案 0 :(得分:2)

这是我的答案:

echo '<p class="games_image_result"><img src="uploaded_images/'.$row["games_image"].'" />';

你有一个语法错误,你在你的php中包含<?php?>,以及你的回声。

所以,当你看看你的img的src时,会有那些。

此外,您忘记关闭img标记和其他p标记。

答案 1 :(得分:0)

您的图片代码未正确关闭,图片路径未完整。您只是指定文件名。我不确定你的目录结构

echo '<p class="games_image_result"><img src="uploaded_images/<?php echo $row["games_image"];?>" />';

答案 2 :(得分:0)

使用php从DataBase显示图像     

           # SQL Statement
           $sql = "SELECT `idimage` FROM `yourTableName` WHERE id=" . mysqli_real_escape_string($_GET['id']) . ";";
           $result = mysqli_query("$sql") or die("Invalid query: " . mysqli_error());

           # Set header
           header("Content-type: image/your image name which type of your image");
           echo mysqli_result($result, 0);
      }
      else
           echo 'Please check the ID!';
 ?>

我希望它可以帮到你