简体   繁体   English

PHP MySQL从数据库显示图像

[英]PHP MySQL display image from database

Hi I have form to make new article with informations and image. 嗨,我有用信息和图像制作新文章的表格。 I susccesfuly save all info and image to database (i guess). 我成功地将所有信息和图像保存到数据库(我猜)。 And when I want to display info and image so only info works. 当我想显示信息和图像时,仅信息有效。 See in picture. 如图所示。

Any help? 有什么帮助吗? Thanks a lot 非常感谢

图片

Inserting_post.php 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>

Here's my answer: 这是我的答案:

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

You got a syntax error where you include a <?php and ?> inside your php, and on your echo at that. 您遇到了语法错误,其中在<?php内包含<?php?> ,并在此处回显。

So when you look at your img's src, it would have those. 因此,当您查看img的src时,它将具有这些内容。

Also, you forgot to close your img tag and your other p tags. 另外,您忘记了关闭img标签和其他p标签。

Your image tag isn't closed properly and the path to the image is not complete. 您的图片标签未正确关闭,并且图片的路径不完整。 You are only specifying the filename. 您仅指定文件名。 I am not sure about your directory structure 我不确定您的目录结构

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

Display Image from DataBase using php 使用PHP从数据库显示图像

           # 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!';
 ?>

I wish it's may be help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM