Log in

View Full Version : Picture displaying help



QuizToon
07-03-2006, 10:31 PM
Hi all

Got 2 questions here.

I have managed, after much reading trial and error to get a form to post a picture into a directory and the path and image name etc in the mysql database, all that is working fine.

I have even managed to get the pictures to get a page working which extracts the information onto the page. Except

question 1 - All of the fields I have asked for, display but the picture is just the place holder with the little red x in the top left corner. The picture is sitting in the correct directory. I have a feeling it is something to do with the path but I dont know. the path is in this file:

<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>

<body>
<?
// you can change this to any directory you want
// as long as php can write to it
$uploadDir = '/home/sites/grap.co.uk/public_html/upload/pictures/';


if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$comment = $_POST['comment'];
// the files will be saved in filePath
$filePath = $uploadDir . $fileName;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

include 'library/config.php';
include 'library/opendb.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path, comment ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$comment')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'library/closedb.php';

echo "<br>File uploaded<br>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><p>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
Select Photograph to upload<input name="userfile" type="file" class="box" id="userfile">
</p>
<p>Comment <textarea rows="10" name="comment" cols="30"></textarea>
</p></td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>

</form>
</body>
</html>

Question 2. - How do I make it so that all of the entries in the database display one below another instead of just the last picture uploaded. Each picture and its comments have an id which is the unique key.

My display file is

<?php
// This is the config
$dbhost = 'localhost';
$dbuser = 'web23-dbase';
$dbpass = 'daddad';
$dbname = 'web23-dbase';

// This is to open the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

//This is the query
$query = "SELECT * FROM upload2";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$text = $row['id'];
$url = $row['path'];
$description = $row['comment'];
}
// This is to close database
mysql_close($conn);
?>

<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><img src="images/<?php echo"$url" ?>" width="250" height="250" alt=""><br><?php echo"$url" ?></td>
<td align="left" valign="top"><?php echo"$description" ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>


If yoou have read this far I thank you very much for your time, I hope someone can shed some light on this for me. I am very new to php and am learning albeit slowly.

Thanks again:)

djr33
07-03-2006, 11:58 PM
You "think" it might be "something to do with the path"... er... just check. This is output to html, so check what's wrong with the path, then fixing shouldn't be hard.
link to the page, and it'll be a lot easier to see what's going on.


As for displaying them all, what you have is a while loop for all the rows, but nothing happens in it... you just get values.
Then after the loop you display all the values, which, obviously, will be the last things assigned within the while loop.
Simply move the display html into the while loop, looping its output, and you're good. Note that you should include a <br> tag or something between each image, but you can probably guess that anyway.


Hope this gets you started.


Also, just for the record, never store a file IN the database. When I saw your title, I was worried. You'd rather want to do what you're doing... and store the path, info, etc. about the file in the DB but have the file as a file, not as an entry in the database. (It's possible to store files as text, but it would just cause everything to run slowly in the DB, etc.)
You're not doing this, nor would I guess that you plan to, but it's good to just mention it in case.

Twey
07-04-2006, 12:23 AM
Also, just for the record, never store a file IN the database. When I saw your title, I was worried. You'd rather want to do what you're doing... and store the path, info, etc. about the file in the DB but have the file as a file, not as an entry in the database.A "file..." you're still thinking statically :) If you're referring to image data, there's absolutely no problem with storing it in a database. That's what the BINARY BLOB type in MySQL is for. :)

QuizToon
07-04-2006, 07:11 PM
Thanks for that

I dont know where to put the table to display the output. When i try i get an error.

//This is the query
$query = "SELECT * FROM upload2";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$text = $row['id'];
$url = $row['path'];
$description = $row['comment'];
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><img src="images/<?php echo"$url" ?>" width="250" height="250" alt=""><br><?php echo"$url" ?></td>
<td align="left" valign="top"><?php echo"$description" ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

}
// This is to close database
mysql_close($conn);
?>

This is what i have done.

I have also checked the path and the files are sitting in the correct directory and the path in the databse is the same as the directory in which the files are sitting. :confused:

Twey
07-04-2006, 07:52 PM
When i try i get an error.Care to tell us what the error is?

QuizToon
07-04-2006, 08:25 PM
actually it is one error after another, of the same kind of thing eg

Parse error: parse error, unexpected '<' in /home/sites/grap.co.uk/public_html/upload/test7.php on line 19

I am now at a point where i just dont understand enough to fix so I am giving up Thanks for your help though.

I wouldnt have thought it was so difficult to display some pictures and some text. I have been trying to sort this for 4 days now. Unfortunately any tutorials I find seem to assume you now what they are talking about in the first place or it doesnt quite fit what I am trying to achieve.

Thanks for trying.

Twey
07-04-2006, 08:27 PM
Ah yes, here we go. You need to have semicolons after those echo statements, it's only when using the short-tag syntax <?=VAR?> that this doesn't apply.

QuizToon
07-04-2006, 10:59 PM
Twey to the rescue

got it thanks

:)