1

How can I show my all "blog post" with images? Example: Mysql table:

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

What is the php code to display all my posts with images in the index page? I used the following code but it doesn't display images, it shows only data. I would like to see something like this:

image | message is here image | message is here image | message is here

I used 3 pages

  1. add_post.php(Html form)
  2. add_post_process.php(process the add_post.php)
  3. index.php (which shows my all post)

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/>";
}
?>

The mysql table structure is

post_id(int)
User_id(int)
subject(varchar)
image(blob)
img_name(varchar)
message(text)
Holger Just
  • 52,918
  • 14
  • 115
  • 123
Shibbir
  • 1,963
  • 2
  • 25
  • 48
  • possible duplicate of [php database image show problem](http://stackoverflow.com/questions/2577416/php-database-image-show-problem) – ChrisWue Dec 10 '11 at 17:35

2 Answers2

0

In your index.php you have <img src="$upload_path" />. Instead of $upload_path, src needs to be the URL to your image.


I just noticed that you are storing the image itself in the database. There is no need to do this if you move it to a publicly accessible directory, e.g. /var/www/images/myimage.jpg

middus
  • 9,103
  • 1
  • 31
  • 33
  • Hi, middus, thanks for your reply...i used this code... echo ''; but it's doesn't show. – Shibbir Dec 10 '11 at 17:41
  • That's probably because `$upload_path` is not a URL, it's a directory on your computer. And because `$img` seems to be a binary blob, not a filename. – middus Dec 10 '11 at 18:08
  • Thanks a lot middus it's working. but it's show too many image, so that i changed my query..here is the query.....$sql= mysql_query("SELECT * FROM blog_post WHERE post_id = '$id' ORDER BY post_id DESC"); BUT it's show blank page. ? – Shibbir Dec 10 '11 at 18:17
  • Hey, I finally Solved my Problem, Thanks, Thanks a lot "middus". – Shibbir Dec 10 '11 at 18:26
0

change

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

to

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

that should do the trick..

I don't now if it matters in performanceways.. but howcome you use blob instead of a varchar.. 255 characters for a filename should be enough.

Burrhus
  • 156
  • 6
  • btw.. are the paths $upload_path = "/secure/content/blogpostimg"; and $upload_path = './blogpostimg/'; equal – Burrhus Dec 10 '11 at 17:49
  • Well Burrhus, It's show something like that [ ·ÿI,è„¡qx:ØÃ ŸI¸÷m律?ò!YôzwýŧþÛgþAQèbn»ú­ü¥i'ý©$9¯a¼Òvg ] Doesn't show image. I used two column name, One is "image(Blob)" and other is img_name(varchar). – Shibbir Dec 10 '11 at 17:52
  • ahhh.. sorry.. my mistake.. the blob has to be converted. http://www.firebirdfaq.org/faq250/ -- I would propably change the type to varchar if possible – Burrhus Dec 10 '11 at 19:10