What I have here is a bulletin board in PHP with the ability to post comments and images. I am able to store my comments and my images to the database. My question involves 2 of my SQL tables, one for stored posts and the other table for storing images. When I echo out my table of posts I would like to echo out the corresponding image inside the post body. I have set up my tables to match with the column attribute of DATETIME but I fail to comprehend how to access my images' table while I am in a foreach loop with my posts table. I have included the code for my index.php file and my getImage.php file. Help would be greatly appreciated.
I made an update to my getImage file in the $ID = $_GET['id']; where 'id' needed to be lower case. I am now getting some images displayed in my table but some only partial. I think I am on my way. I know I need an if statement to determine if there is an image in the post before displaying. thanks for all the help.
<?php
include('connect.php');
$query = 'SELECT * FROM forumPosts LEFT JOIN PostImages ON
forumPosts.DATETIME = PostImages.ImageDATETIME ORDER BY replyIndex
ASC';
$statement = $db->prepare($query);
$statement->execute();
$posts = $statement->fetchAll();
$statement->closeCursor();
echo "<table>";
foreach ($posts as $post){
if ($post['post_type'] == "r"){
$post_id = $post['post_id'];
echo "<tr bgcolor='beige'><td>reply</td><td>".
$post['post_title'] . "</td ><td>". $post['post_body'] . "<img
src='getImage.php?id=".$post['ID']."'>". "</td><td>".
$post['DATETIME']. "</td><td>". $post['replyIndex']. "</td>
<td>".$post['post_type']."</td>";
?>
<?php
include('connect.php');
$ID = $_GET['id'];
$query = "SELECT * FROM PostImages WHERE ID=:ID";
$statement = $db->prepare($query);
$statement->bindvalue(':ID', $ID);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
header("Content-type: image/jpeg");
echo $row['image'];
?>