Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-18-2012, 07:53 AM   PM User | #1
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
Question How to display image from a database

i have a script i wrote that uploads an image to the database then moves it to a directory leaving only image path in the database but when i try to display the image i does not show any help appreciated. Thanks

Upload script
PHP Code:
if(isset($_POST['subbutton'])){

$name $_POST['namet'];
$email $_POST['emailt'];
$about $_POST['about'];
$location $_POST['locationt'];
$website $_POST['websitet'];
$comment $_POST['commentt'];

        
$uploadDir 'wp-content/themes/speedy/testimonial/'//Image Upload Folder
        
        
$allowed_ext =  array('jpg''jpeg''png''gif');
        
        
$fileName addslashes($_FILES['pict']['name']);
        
$tmpName  $_FILES['pict']['tmp_name'];
        
$fileSize $_FILES['pict']['size'];
        
$fileType $_FILES['pict']['type'];
        
$file_ext strtolower(end(explode('.'$fileName)));
        
$filePath addslashes($uploadDir $fileName);
        
$result move_uploaded_file($tmpName$filePath);

if (
in_array($file_ext$allowed_ext) === false){

}        
if(
$name!="" && $email!="" && $comment!=""){
    
$query="INSERT INTO testimonial(id, name, email, about, location, website, picture, comment)
              VALUES ('', '$name', '$email', '$about', '$location', '$website', '$filePath', '$comment')"
;
              
mysql_query($query)  or die(mysql_error());
              echo 
"<br/><b style='color:green'>Your testimonial has been received</b>";
}else{
  echo 
"<br/><b style='color:red;'>there was an error.</b>";
}
  
$to "dianreidreid@gmail.com";
$subject "Testimonial approval request";
$name $_POST['namet'];
$email $_POST['emailt'];
$comment $_POST['commentt'];
$message "
Name: $name $email<br/>
Email: $email_field<br/>
Message: $comment"
;
$headers  'MIME-Version: 1.0' "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
// Additional headers
/*$headers .= 'To: <[email address goes here]>' . "\r\n";
$headers .= '[From] <[email address goes here]>' . "\r\n";*/
// Mail it
mail($to$subject$message$headers);    
  

This is how i try to display the image
PHP Code:
 $per_page = 4;

                        $pages_query = mysql_query("SELECT COUNT(id) FROM testimonial")or die(mysql_error());
                        $pages = ceil(mysql_result($pages_query, 0) / $per_page);

                        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
                        $start = ($page - 1) * $per_page;
                        
                        $query = mysql_query("SELECT name, comment, picture FROM testimonial LIMIT $start, $per_page ");
                        $count=mysql_num_rows($query);
                        
                        $pic=file_exists($query_row['picture']);
                        
                        $count=1;
                        while ($query_row = mysql_fetch_array($query)){
                        ?>
                        
                      <?php if($count==1){ ?>
                        <div class="testmon_box">
                      <?php ?>
                      <?php if($count<=1){ ?>    
                      <div class="TESMON_INNERBOX">
                       
                         
                                
                     [COLOR="Blue"] <div class="pic_block">
                            <?php if($pic !=""){?>
                                <img src="<?php echo $pic?>" />
                            <?php } else{?><img src="<?php bloginfo('template_url'); ?>/images/pic_02.jpg" /><?php ?>
                            
                      </div>[/COLOR]
                        
                       <div class="test_cont_block">

                          <p> <?php echo $result=$query_row['comment']; ?></p>

                         <span class="danam"> 
                        Date of Posting: <?php echo date('l, F j, Y'); ?><br/>

                        <strong>Posted by: <?php echo $result=$query_row['name']; ?></strong>

                         </span>
                         
                       </div>
                            
                             
                             
                       <div class="clear"> </div>

                        </div>

                    

                        </div>
                       <?php ?>
                              
                       <?php ?>
                     
                      <div class="pagin_nav">
                         <?php if ($pages >=&& $page <= $pages){
                           for (
$x=1$x<=$pages$x++){
                                echo (
$x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></srtong> ' '<a href="?page='.$x.'">'.$x.'</a> ';
                              
                           }
                         }
?>
gazaian1 is offline   Reply With Quote
Old 09-18-2012, 10:55 AM   PM User | #2
vnbenny88
New Coder

 
Join Date: Sep 2012
Posts: 11
Thanks: 0
Thanked 1 Time in 1 Post
vnbenny88 is an unknown quantity at this point
Please tell me, what path is saved in your database?
vnbenny88 is offline   Reply With Quote
Old 09-18-2012, 12:43 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
I just want to clarify something here.. from this code you're storing the image path to a file on the disk yet in your previous topic you state that you are using a blob and can see the blob size? Which is it and did you amend the data if you've restructured your columns? - I only ask because if picture is still containing blobs and you're rying to print file paths..
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
gazaian1 (09-20-2012)
Old 09-18-2012, 04:47 PM   PM User | #4
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
I just want to clarify something here.. from this code you're storing the image path to a file on the disk yet in your previous topic you state that you are using a blob and can see the blob size? Which is it and did you amend the data if you've restructured your columns? - I only ask because if picture is still containing blobs and you're rying to print file paths..

Sorry i am just a bit confused i only see BLOB and a size in the database. Does that mean their is nothing stored.
gazaian1 is offline   Reply With Quote
Old 09-18-2012, 05:16 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Right so if you're seeing a blob then you have a field that is taking blob data.

What are you putting into it - from the code you've shown above, it looks like you're storing the filename and path yet from the code in your previous topic you were storing the file itself in the blob field. Which is it?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
gazaian1 (09-20-2012)
Old 09-18-2012, 05:27 PM   PM User | #6
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
Right so if you're seeing a blob then you have a field that is taking blob data.

What are you putting into it - from the code you've shown above, it looks like you're storing the filename and path yet from the code in your previous topic you were storing the file itself in the blob field. Which is it?
Yes i want to store the file path in the database which is link to a directory on my server. The images are stored on the server when uploaded but i can't get it to display on the front end. How can i achieve this please also how can i my the upload field not required.
gazaian1 is offline   Reply With Quote
Old 09-18-2012, 06:10 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Ok so you need to test the file path to the image then. You're not far from it, you've got it printing the file path into your html so now you just need to figure out whats wrong with the final path in the html.

You can copy it out and add it onto the url in the browser bar - thats one way of testing it.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
gazaian1 (09-20-2012)
Old 09-18-2012, 06:36 PM   PM User | #8
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
Ok so you need to test the file path to the image then. You're not far from it, you've got it printing the file path into your html so now you just need to figure out whats wrong with the final path in the html.

You can copy it out and add it onto the url in the browser bar - thats one way of testing it.

Thanks for your help so far here is a link to my site:
http://speedycarloans.ca/testimonials/

Right now it only display the default image because i used an if statement to check if the database is empty or not.
gazaian1 is offline   Reply With Quote
Old 09-18-2012, 07:15 PM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by gazaian1 View Post
Right now it only display the default image because i used an if statement to check if the database is empty or not.
Well that won't really help much. Unless you print the path to the image in the html, how do you expect to test it?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
gazaian1 (09-20-2012)
Old 09-18-2012, 08:21 PM   PM User | #10
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
I wouldn't store the path to the image in the database, just the file name - with or without the ext. Ex: ny-city-9456.jpg or ny-city-9456

The image path should be in the while loop that queries the database.



----
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Users who have thanked Len Whistler for this post:
gazaian1 (09-20-2012)
Old 09-18-2012, 09:05 PM   PM User | #11
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
Quote:
Originally Posted by Len Whistler View Post
I wouldn't store the path to the image in the database, just the file name - with or without the ext. Ex: ny-city-9456.jpg or ny-city-9456

The image path should be in the while loop that queries the database.



----
Ok when i try to store the file path or name i only see this in the database: [BLOB - 59B]
gazaian1 is offline   Reply With Quote
Old 09-18-2012, 09:13 PM   PM User | #12
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Quote:
Originally Posted by gazaian1 View Post
Ok when i try to store the file path or name i only see this in the database: [BLOB - 59B]

The MySQL data type should be something like VARCHAR or TINYTEXT maybe 30 characters for an image file name. You don't need BLOB.





---
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Users who have thanked Len Whistler for this post:
gazaian1 (09-20-2012)
Old 09-18-2012, 09:15 PM   PM User | #13
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by gazaian1 View Post
Ok when i try to store the file path or name i only see this in the database: [BLOB - 59B]
As I asked you earlier, did you restructure the database (obviously no) and did you change the contents from the file itself to the filename and path instead? - Clearly this looks like no too.

You posted another topic today. In that code you were putting the actual file inside the database. Since then you've decided to use the filename / path.

When you change your mind and decide to use the filename instead of the file stream, you must delete the old files that are stored in the database and change the column from a blob to a text field instead.

I suspect the reason none of your images are showing is because instead of printing a file name and path in your html, you're trying to print a filestream instead.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
gazaian1 (09-20-2012)
Old 09-18-2012, 09:36 PM   PM User | #14
gazaian1
New Coder

 
Join Date: Aug 2011
Posts: 52
Thanks: 24
Thanked 0 Times in 0 Posts
gazaian1 is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
As I asked you earlier, did you restructure the database (obviously no) and did you change the contents from the file itself to the filename and path instead? - Clearly this looks like no too.

You posted another topic today. In that code you were putting the actual file inside the database. Since then you've decided to use the filename / path.

When you change your mind and decide to use the filename instead of the file stream, you must delete the old files that are stored in the database and change the column from a blob to a text field instead.

I suspect the reason none of your images are showing is because instead of printing a file name and path in your html, you're trying to print a filestream instead.
Sorry i am kind of new to this.

i changed the type to varchar with a length of 255 characters but till no image.
this is what i have for my output code now
PHP Code:
    <?php   
                        $per_page 
4;

                        
$pages_query mysql_query("SELECT COUNT(id) FROM testimonial")or die(mysql_error());
                        
$pages ceil(mysql_result($pages_query0) / $per_page);

                        
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
                        
$start = ($page 1) * $per_page;
                    
                        
$query mysql_query("SELECT id, name, comment, picture FROM testimonial LIMIT $start, $per_page") or die(mysql_error());
                        
$count=mysql_num_rows($query);
                        
                        
                        
                        
                        
$count=1;
                        while (
$query_row mysql_fetch_array($query)){
                        
$pic=$query_row['picture'];
                        
?>
                        
                      <?php if($count==1){ ?>
                        <div class="testmon_box">
                      <?php ?>
                      <?php if($count<=1){ ?>    
                      <div class="TESMON_INNERBOX">
                       
                         
                                
                      <div class="pic_block">
                            <?php if($pic !=""){?>
                                <img src="<?php echo $pic?>" />
                            <?php } else{?><img src="<?php bloginfo('template_url'); ?>/images/pic_02.jpg" /><?php ?>
                            
                      </div>
                        
                       <div class="test_cont_block">

                          <p> <?php echo $result=$query_row['comment']; ?></p>

                         <span class="danam"> 
                        Date of Posting: <?php echo date('l, F j, Y'); ?><br/>

                        <strong>Posted by: <?php echo $result=$query_row['name']; ?></strong>

                         </span>
                         
                       </div>
                            
                             
                             
                       <div class="clear"> </div>

                        </div>

                    

                        </div>
                       <?php ?>
                              
                       <?php ?>
                     
                      <div class="pagin_nav">
                         <?php if ($pages >=&& $page <= $pages){
                           for (
$x=1$x<=$pages$x++){
                                echo (
$x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></srtong> ' '<a href="?page='.$x.'">'.$x.'</a> ';
                              
                           }
                         }
?>
gazaian1 is offline   Reply With Quote
Old 09-18-2012, 09:43 PM   PM User | #15
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Quote:
Originally Posted by gazaian1 View Post
Sorry i am kind of new to this.

i changed the type to varchar with a length of 255 characters but till no image.
this is what i have for my output code now
A good way to debug PHP is to View Page Source and see what is missing in the HTML. I use FireFox to view the page source, I don't know about IE or Opera.



---
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Users who have thanked Len Whistler for this post:
gazaian1 (09-20-2012)
Reply

Bookmarks

Tags
html, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.