Page 1 of 1

Display image from database into HTML table

Posted: Thu Apr 26, 2012 12:32 am
by Jackpotgee
Hi all, If you can help me i'd be a very happy man. Been trying to get this working for too long now.

I have an upload form where the user can upload an image.

the form is
[text]<form id="newaddform" action="newaddsubmit.php" method="post" enctype="multipart/form-data">

the image fields is
<input type="file" name="mainimage" class="imginput"/>

The upload script appears to be working fine with the main data being inserted into a table called ads, and the image data being inserted into a table called 'images'
the images table stores
img_id - AI
name - var
type - var
size - int
image - blob

the data for the above table is created using

Code: Select all

//Set variables for img1
		$img1_name=$_FILES['mainimage']['name'];
		$img1_type=$_FILES['mainimage']['type'];
		$img1_size=$_FILES['mainimage']['size'];
		$img1_temp=$_FILES['mainimage']['tmp_name'];
		$img1_err=$_FILES['mainimage']['error'];
		
		//Set array containing allowable file types
		$files = array ("image/gif", "image/jpg", "image/bmp", "image/jpeg"); 
		//Check that file is of allowable type
		if (!in_array($img1_type,$files))
			{
			die ("Sorry the requested file was not an allowable image type");
			}
		
		//Double Check file exists on server before transfer to DB
		if (!file_exists($img1_temp))
			{
			die ("Sorry we've had trouble uploading your file");
			}				
		//Read file from server and assign to content var
		$fp = fopen($img1_temp,"r") or die("error");
		$img1_cont = fread($fp,filesize($img1_temp)) or die("error");
		$img1_cont = addslashes($img1_cont);
		fclose($fp);
Now my problem is, how to i display this image in a cell in a HTML table???
I've tried loads of different variations but cant get the image to display properly. I'm presuming it's because i am not giving the browser all the information needed to display the image... Currently i am just getting a load of gibberish displayed in the cell!

Current code:

Code: Select all

echo "<tr><th>Ad Title</th>";
			echo "<th>Image</th>";
			echo "<th>Main Text</th></tr>";
			
			//Get Ad information
			$id = $_SESSION['user_id'];
			//$adsSQL = "SELECT * FROM ads WHERE member_id = $id";
			$adsSQL = "SELECT * FROM ads a LEFT OUTER JOIN images i ON i.img_id = a.imgone WHERE member_id = $id";
			$result =mysql_query($adsSQL);
			
			//Populate table with info from query
			While ($row=mysql_fetch_array($result))
				{
				echo "<tr><td>".$row['title']."</td>";
				
				echo "<td>";
				echo "<img src=/'".$row['image']."'/>";
				echo "</td>";
				echo "<td>".$row['body']."</td>";
				}
				
			echo "</table></p>";

Any help would be greatly appreciated! Thanks!

Re: Display image from database into HTML table

Posted: Thu Apr 26, 2012 9:19 am
by Christopher
You need to create a separate script that fetches the record from the database, sets the MIMI type header and outputs the image data. You should save the image type in a field in the record. Then in your table, do something like:

Code: Select all

echo '<img src="showimage.php?id='.$row['id'].'"/>';