Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts

    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> ';
                                  
                               }
                             }
    ?>

  • #2
    New Coder
    Join Date
    Sep 2012
    Location
    Murcia, Spain
    Posts
    21
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Please tell me, what path is saved in your database?

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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..
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    gazaian1 (09-20-2012)

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    gazaian1 (09-20-2012)

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    gazaian1 (09-20-2012)

  • #8
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    gazaian1 (09-20-2012)

  • #10
    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
    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

  • Users who have thanked Len Whistler for this post:

    gazaian1 (09-20-2012)

  • #11
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts
    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]

  • #12
    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
    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

  • Users who have thanked Len Whistler for this post:

    gazaian1 (09-20-2012)

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    gazaian1 (09-20-2012)

  • #14
    New Coder
    Join Date
    Aug 2011
    Posts
    52
    Thanks
    24
    Thanked 0 Times in 0 Posts
    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> ';
                                  
                               }
                             }
    ?>

  • #15
    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
    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

  • Users who have thanked Len Whistler for this post:

    gazaian1 (09-20-2012)


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •