来自Mysql数据库的“博客文章”显示

时间:2011-12-10 17:24:06

标签: php mysql

如何用图片显示我的所有“博文”?例: Mysql表:

post_id | user_id | subject | message | image | img_name.

在索引页面中显示我所有帖子的PHP代码是什么?我使用以下代码,但它不显示图像,它只显示数据。我想看到这样的事情:

  

图片|消息在这里   图片|消息在这里   图片|消息在这里

我用了3页

  1. add_post.php(Html表格)
  2. add_post_process.php(处理add_post.php)
  3. index.php(显示我的所有帖子)
  4. add_post_process.php:

    <?php
    include "db.php";
    @$file = addslashes($_FILES['image']['tmp_name']);
    $lastid= mysql_insert_id();
    @$img = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $img_name = addslashes($_FILES['image']['name']);
    @$img_size = getimagesize($_FILES['image']['tmp_name']);
    $upload_path = './blogpostimg/';
    $post_id = addslashes($_POST['post_id']);
    $user_id = addslashes($_POST['user_id']);
    $subject = addslashes($_POST['subject']);
    $message = addslashes($_POST['message']);
    $cat_id = addslashes($_POST['cat_id']);
    $cat_name = addslashes($_POST['cat_name']);
    $comment_count = addslashes($_POST['comment_count']);
    $ch_img = addslashes($_FILES['image']['name']);
    $query = "SELECT img_name FROM blog_post WHERE img_name = '$ch_img';";
    $result = mysql_query($query) or die (mysql_error());
    if(isset($subject, $message))
    {
      $errors = array();
      if(empty($subject) && empty($message) && empty($file))
      {
        $errors[] ='all field required';
      }
      else
      {
        if(empty($subject))
          $errors[] = 'Subejct requried';
        if (empty($message))
          $errors[] = 'message required';
        if(empty($file))
          $errors[] ="SORRY, you have to be upload a image";
        elseif($img_size == FALSE)
        {
          $errors[] ="That's not an image";
        }
        else
        {
          if(mysql_num_rows($result) != 0)
          $errors[] = " You have to change this image name, already exit in our database";
        }
      }
      if(!empty($errors))
      {
        foreach($errors as $error)
        {
          echo "<ul>";
          echo "<strong><font color=red><li>$error</li></font></strong><br/>";
          echo "</ul>";
        }
      }
      else
      {
        if(!move_uploaded_file($_FILES['image']['tmp_name'],$upload_path . $img_name))
        {
          die('File Not Uploading');
        }
        else
        {
          $lastid = mysql_insert_id();
          $query = mysql_query("INSERT INTO blog_post VALUES ('', '',   '$subject',
            '$img','$img_name','$message','$cat_id','$cat_name','',NOW() )");
          if($query)
            echo "Successfully uploaeded your post";
          else
          {
            echo "Something is wrong to Upload";
          }
        }
      }
    }
    ?>
    

    的index.php

    <?php
    include "db/db.php";
    $upload_path = "/secure/content/blogpostimg";
    $sql= mysql_query("SELECT * FROM blog_post");
    while ($rel = mysql_fetch_assoc($sql))
    {
      $id = $rel['post_id'];
      $sub = $rel['subject'];
      $imgname = $rel['img_name'];
      $img = $rel ['image'];
      $msg = $rel['message'];
      $date = $rel['date'];
      echo "<h1>". "$sub" ."</h1>". "<br/>";
      echo "$imgname" ."<br/>";
      echo '<img src="$upload_path " />';
      echo "$msg" . "<br/>";
      echo "$date" . "<br/>";
      echo "<hr/>";
      echo "<br/>";
    }
    ?>
    

    mysql表结构是

    post_id(int)
    User_id(int)
    subject(varchar)
    image(blob)
    img_name(varchar)
    message(text)
    

2 个答案:

答案 0 :(得分:0)

在index.php中,您有<img src="$upload_path" />。 <{1}}取代$upload_path,而src必须是图片的网址。


我刚刚注意到您将图像本身存储在数据库中。如果将其移动到可公开访问的目录,则无需执行此操作,例如, /var/www/images/myimage.jpg

答案 1 :(得分:0)

变化

echo '<img src="$upload_path " />';

echo '<img src="' . $upload_path . '/' . $img . '" />';

应该做的伎俩..

我现在不知道如果它在性能方面很重要..但是你怎么使用blob而不是varchar ..文件名的255个字符就足够了。