If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > NEWBIE Need Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-13-05, 00:17
dgallig dgallig is offline
Registered User
 
Join Date: Dec 2005
Posts: 3
Red face NEWBIE Need Help

Hi all ,
Since a week i decide to learn HTML ,PHPand MYSQL ...
So I'm REALLY new to this .
My first work has been making a guestbook , I managed to make it works , but then
I tried to upload and then display images with the messages , and I failed ... Since 2 days I'm trying to find out how to do it , but without any success .
(Probably I messed up something very easy ...:P )
If some can help me , I'd really appreciate it .
So those are my files :
The form :
-------------------------------------------------------------------------------------
<form enctype="multipart/form-data" action="write2db.php" method="post">
<fieldset>
<legend> Write here </legend><br>

Nome : <input type="text" name="name" rows="10" columns="6" ><br>
Soggetto: <input type="text" name="subject" rows="10" columns="5" ><br>
<input type="hidden" name="MAX_FILE_SIZE" value="320000">
Image :<input name="photo" type="file"><br>
<p>Messaggio :</p>
<TEXTAREA name="message" rows="10" cols="22" > </textarea>
<input type="submit" value="Submit">
<input type="reset" name="resetbutton" value="Cancel">
</fieldset>
</form>
---------------------------------------------------------------------------

The script to upload :
---------------------------------------------------------------------------------
<?
$photo = $_FILES['photo'];
$date= Date("j/d/Y H:i" );
$name = $_POST['name'] ;
$subject = $_POST['subject'] ;
$message = $_POST['message'] ;
$esc_name = mysql_escape_string($name);
$esc_subject = mysql_escape_string($subject);
$esc_msg = mysql_escape_string($message);

mysql_connect("localhost", "admin", "password") or die(mysql_error());
mysql_select_db("guestbook") or die(mysql_error());
mysql_query("INSERT INTO guestbook
(name,date,subject,photo,message)VALUES('$esc_name ', '$date','$esc_subject','$photo','$esc_msg') ")
or die (mysql_error()) ;

header("location:http://caffeina/prove/index.php?p=msgdb");
?>
-------------------------------------------------------------------------------------

and then to display :
------------------------------------------------------------------------------------
<?
echo "<h2>" ;
echo "Guestbook" ;
echo "</h2>" ;
mysql_connect("localhost", "admin", "password") or die(mysql_error());
mysql_select_db("guestbook") or die(mysql_error());
$result = mysql_query("SELECT * FROM guestbook ORDER BY id DESC")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
$id = $row['id'];

echo "<div id=\"msg_header\">";
echo "Msg. nº :",$row['id'];
echo " on date ",$row['date'];
echo "<br>";
echo "<br>";
echo "..:",$row['name'];
echo ":..";
echo "<br>";
echo "<br>";
echo $row['subject'];
echo "<br>" ;
echo "<br>" ;
echo "</div>";
echo "<div id=\"msg_body\">";
echo "<br>" ;
echo "<br>" ;
#echo $row['photo'] ;
echo $row['message'];
echo "<img src=\"image.php?file=$id\" =\"170\" height=\"127\" border=\"1\" >";
#echo $row['photo'];
echo "<br>" ;
echo "<br>" ;
echo "</div>";}
?>

and

<?

mysql_connect("localhost", "admin", "password") or die(mysql_error());
mysql_select_db("guestbook") or die(mysql_error());
$result = mysql_query("SELECT photo FROM guestbook WHERE id = '.$_GET[id].' ")
or die(mysql_error());
header("Content-Type: image/jpeg");
$r=mysql_query($result);
$row=mysql_fetch_row($r);
echo $row['photo'];

?>
-------------------------------------------------------------------------------------
From th db :
-------------------------------------------------------------------------------------
mysql> select * from guestbook ;
+----+-----------+------------------+---------+-------+---------+
id | name | date | subject | photo |message
+----+-----------+------------------+---------+-------+----------+
| 1 | David | 13/13/2005 05:16 | TEST1| | aa |
| 2 | SDL | 13/13/2005 05:18 | TEST2| Array | test |


THX

David
Reply With Quote
  #2 (permalink)  
Old 12-13-05, 09:13
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
What is the error you are getting? Was this ever working with the images? (Is that how you output and create images from a database in php?? Never tried it) Does the image.php page work if you navigate directly to it? Also, please post your table structure.
Reply With Quote
  #3 (permalink)  
Old 12-13-05, 11:56
dgallig dgallig is offline
Registered User
 
Join Date: Dec 2005
Posts: 3
Hi ,

This is the output

<h2>Guestbook</h2><div id="msg_header">Msg. nº :8 on date 13/13/2005 06:49<br><br>..DL:..<br><br>Secondo post di prova un po piú lungo del primo p<br><br></div><div id="msg_body"><br><br> sdasd<img src="image.php?file=Array" ="170" height="127" border="1" ><br><br></div>


Well , the form and the upload script should upload image to the db ... same way as the data .
( maybe I'm wrong :P ).Image.php doesn't work .
Already posted table content ...

thx
Reply With Quote
  #4 (permalink)  
Old 12-13-05, 13:29
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
So everything is being inserted correctly, right? It's just retrieving the images that is the problem?

Quote:
<img src="image.php?file=Array"
Hmmm...I'm not sure why this is happening since the $id variable is working in the rest of the script? "file" should equal the guestbook entry id, right?


Quote:
$result = mysql_query("SELECT photo FROM guestbook WHERE id = '.$_GET[id].' ")
Sinc you are passing the id as the variable file in the query string (image.php?file=...) change that php line to:

Code:
$result = mysql_query("SELECT photo FROM guestbook WHERE id = '" . $_GET["file"] . "' ")
Hopefully those changes will get us closer.
Reply With Quote
  #5 (permalink)  
Old 12-13-05, 17:34
dgallig dgallig is offline
Registered User
 
Join Date: Dec 2005
Posts: 3
Many thx jfulton ...
Finally I got it working !
Seems the problem was just here :

$photo_name = $_FILES['photo']['name'];
$temp_photo = $_FILES['photo']['tmpname'];
$photo_size = $_FILES['photo']['size'];
$photo_type = $_FILES['photo']['type'] ;

and not just $photo = $FILES['photo'];
'cause this is just an Array
This for upload correctly the data , and :

echo "<img src=\"".$row['photo_name']."\" border=\"1\" >";

For showing them in the web . That's all ... No need of image.php
( I know i messed up something ... but u know ... first week of this stuff ...)

So I got it working on my machine , but when i moved all the stuff to the server seems that can't find the images ...

Dunno why ... Could be because in the server this stuff it's in UserDir directory and not in the main Apache one ?

I'm investigating it now ...

David
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On