Jump to content

photo display mysql php


tracy

Recommended Posts

I can upload a file to the server and mysql table using php
no problem

when I display the table using php, no photo, only photo name

any thoughts? 

here is the display code for this example...

<?php
include ("link.php");

if($query= mysql_query("SELECT * FROM inventory"))
{
  $num = mysql_num_rows($query); 
}else
{
  die('There was an error with the query:<br>'.mysql_error());


if ($num == '0') {
echo "Nothing Exist.";
die();
}
else {

?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td><font face=Arial>Stock#</font>
</td>
<td><font face=Arial>Year</font>
</td>
<td><font face=Arial>Make</font>
</td>
<td><font face=Arial>Model</font>
</td>
<td><font face=Arial>Price</font>
</td>
<td><font face=Arial>Miles</font>
</td>
<td><font face=Arial>Photo</font>
</td>
</tr>
<?

while ($info = mysql_fetch_array($query)) {
$stock = $info['stock'];
$year = $info['year'];
$make = $info['make'];
$model = $info['model'];
$price = $info['price'];
$miles = $info['miles'];
$photo1 = $info['photo1'];
echo ("
<tr>
<td> <font face=Arial>$stock</font>
</td>
<td> <font face=Arial>$year</font>
</td>
<td> <font face=Arial>$make</font>
</td>
<td> <font face=Arial>$model</font>
</td>
<td> <font face=Arial>$price</font>
</td>
<td> <font face=Arial>$miles</font>
</td>
<td> <font face=Arial>$photo1</font>
</td>
</tr>
");
}
?>
</table>
<?
}
?>

Link to comment
Share on other sites

How are you inserting the photo into the database. You cant just insert it into the database, it has to be read and THEN inserted. e.g:
[code] $filename = $_FILES["upload"]["name"];
$filetype = $_FILES["upload"]["type"];
$filesize = $_FILES["upload"]["size"];
$tmpname = $_FILES["upload"]["tmp_name"];

$open = fopen($tmpname,"r");
$file = fread($open, filesize($tmpname));
$file = addslashes($file);

$addtodb = "insert into uploads (name,type,size,upload) values ('$filename','$filetype','$filesize','$file')";[/code]
Link to comment
Share on other sites

I use this code to insert the file:

<html>
<title>Add Vehicle Form</title>
<body>

<form action="insert.php" method="post">
<table>
<tr>
<td>Stock Number:</td><td><input type="varchar" name="stock" /></td>
</tr>
<td>Year:</td><td><input type="tinyint" name="year" /></td>
</tr>
<td>Make:</td><td><input type="text" name="make" /></td>
</tr>
<td>Model:</td><td><input type="varchar" name="model" /></td>
</tr>
<td>Price:</td><td><input type="tinyint" name="price" /></td>
</tr>
<td>Miles:</td><td><input type="tinyint" name="miles" /></td>
</tr>
<td>Photo1:</td><td><input type="file" name="photo1" /></td>
</tr>

<input type="submit" />
</form>


</body>
</html>
Link to comment
Share on other sites

Presuming you want the photo where $photo1 is, and assuming that the database holds the link to the photo, this is what you need:
<img src=$photo1 alt=$model>

[code]<?php
echo ("
<tr>
<td> <font face=Arial>".$stock."</font>
</td>
<td> <font face=Arial>".$year."</font>
</td>
<td> <font face=Arial>".$make."</font>
</td>
<td> <font face=Arial>".$model."</font>
</td>
<td> <font face=Arial>".$price."</font>
</td>
<td> <font face=Arial>".$miles."</font>
</td>
<td> <font face=Arial><img src=".$photo1." alt=".$model.">
</font>
</td>
</tr>
");
?>[/code]
Link to comment
Share on other sites

Would you explain the 'database holds the link to the file' part?  I thought the database would hold the photo file, much like it holds other data.  I want to display using several sizes.  Is there a better method?

I guess to answer your question, yes, the photo1 cell holds the file I uploaded for the photo...thanks.

[quote author=Cagecrawler link=topic=118457.msg484049#msg484049 date=1166017510]
Presuming you want the photo where $photo1 is, and assuming that the database holds the link to the photo, this is what you need:
<img src=$photo1 alt=$model>

[code]<?php
echo ("
<tr>
<td> <font face=Arial>".$stock."</font>
</td>
<td> <font face=Arial>".$year."</font>
</td>
<td> <font face=Arial>".$make."</font>
</td>
<td> <font face=Arial>".$model."</font>
</td>
<td> <font face=Arial>".$price."</font>
</td>
<td> <font face=Arial>".$miles."</font>
</td>
<td> <font face=Arial><img src=".$photo1." alt=".$model.">
</font>
</td>
</tr>
");
?>[/code]
[/quote]
Link to comment
Share on other sites

I don't really have to store the photo in the database, and I think I'd be better off if I did not store it there, only the link.  That's what I'm confused about.  Where should I store the photo?  Simply in a file called photos, for example?  Then if it's in the same directory as the php code that 'calls' it, it works...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.