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.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Retrieving and echo'ing image from DB issues

    Hi everyone,

    I'm hoping to get some assistance with echoing an image that i've sent to the database. I cannot seem to get the image to display.

    Firstly, Im just doing the basic upload. Due to other parts of the code, I have connected to the database to echo results already in the database (based on ID Auto increments) at this stage an image has not been uploaded.
    PHP Code:
    <?php
    if(isset($_SESSION['username']))
    {
    $con mysql_connect("localhost","root","");
    mysql_select_db("realestate"$con);

    $id=$_POST['userid'];
    $query "SELECT * FROM individual_profile WHERE id = '$id'";


    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
            
    $id $query_row['id'];
            
    $fname $query_row['fname'];
            
    $lname $query_row['lname'];
            
    $email $query_row['email'];
            
    $no $query_row['no'];
            
    $street $query_row['street'];
            
    $suburb $query_row['suburb'];
            
    $postcode $query_row['postcode'];
            
    $home $query_row['homeph'];
            
    $mobile $query_row['mobile'];
            
    $twitter $query_row['twitter'];
            
    $facebook $query_row['facebook'];
            
    $website $query_row['website'];
            
    $description $query_row['profiledescription'];
            
    $image $query_row["profileimage"];
            
      
     echo
    '
       <form name="addindividual" method="post" action="update.php" enctype="multipart/form-data" onsubmit="return validation()">
    <input type="hidden" name="userid" value="'
    .$id.'"/>
    <input type="file" class="uploadimageform" name="profileimage" tabindex="13"/>
    <img src="update.php?id=$id" class="showimage"/>
    <input type="submit" value="Update Profile" name="submit">'
    ;
     }
    }
    Secondly, once the file is selected and submitted, it takes you to a update.php file where data updates the DB based on ID
    PHP Code:
    if(isset($_SESSION['username']))
    {
    $id $_POST['id'];
    $profilename mysql_real_escape_string($_FILES['profileimage']['name']);
    $profiledata mysql_real_escape_string(file_get_contents($_FILES['profileimage']['tmp_name']));
    $profiletype mysql_real_escape_string($_FILES['profileimage']['type']);

    $query="UPDATE individual_profile SET fname='$fname', lname='$lname', no='$no', street='$street', suburb='$suburb', postcode='$postcode', email='$email', homeph='$homeph', mobile='$mobile', twitter='$twitter', facebook='$facebook', website='$website', profiledescription='$profile', profilename='$profilename', profileimage='$profiledata' WHERE id='$id'";

    header('refresh: 0; URL=../profile.php');

    header("content-type: image/jpg"); 
    }

    Lastly, once the data is updated it takes you back to the edit_profile.php file where i'm trying to display the image in an img src (from research) using update.php where i've identified the content-type: image/jpg. The width of the image is appearing however not the physical image. Could anyone possibly assist me in what I am doing wrong?
    PHP Code:
    <img src="update.php?id=$id" class="showimage"/> 

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Here's one thing ...

    You are doing this in your script:
    $id = $_POST['id'];

    But this is in your 'src' tag:
    update.php?id=$id

    That's not a $_POST ... it's a $_GET
    $id = $_GET['id'];


    On the edit_profile.php script, how does it know what $id is?
    I'm assuming there's a lot of other scripting that you are not showing?
    Last edited by mlseim; 04-08-2014 at 11:58 PM.

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'll do my best to include as much detail as possible.

    First, you will start at a profile page where all profiles are displayed as below.

    When the edit button is clicked, the below code comes into play
    PHP Code:
    <?php

    if(isset($_SESSION['username']))
    {
    $con mysql_connect("localhost","root","");
    mysql_select_db("realestate"$con);
    $query "SELECT * FROM individual_profile WHERE username = '$username'";


    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
            
    $id $query_row['id'];
            
    $fname $query_row['fname'];
            
    $lname $query_row['lname'];
            
    $email $query_row['email'];

            echo 
    "<strong>$fname "."$lname</strong></b>" '<br>' "$email";
            echo 
    '<form method="POST" action="addprofile/edit_profile.php">';
            echo  
    '<input type="hidden" name="userid" value="'.$id.'"/>';
            echo 
    '<input type="submit" value="Edit" name="Edit" class="edit">';
            echo 
    "</form>\n";
            echo 
    '<form method="POST" action="addprofile/delete.php" >';
            echo  
    '<input type="hidden" name="userid" value="'.$id.'"/>';
            echo 
    '<input type="submit" value="Delete" class="delete" name="Delete">';
            echo 
    "</form>\n";
            echo 
    '<br>';
            
            }

    }
    }
    ?>
    This echo's the profiles then POST's the id ($id = $_POST['userid']) which is a hidden value.

    Part 2: You then comes to the edit_profile page. As the ID is posted, I've connected to the database and obtained details based on the ID
    PHP Code:
       <?php

    $con 
    mysql_connect("localhost","root","");
    mysql_select_db("realestate"$con);
    $id=$_POST['userid'];
    $query "SELECT * FROM individual_profile WHERE id = '$id'";

    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
            
    $id $query_row['id'];
            
    $fname $query_row['fname'];
            
    $lname $query_row['lname'];
            
    $email $query_row['email'];
            
    $no $query_row['no'];
            
    $street $query_row['street'];
            
    $suburb $query_row['suburb'];
            
    $postcode $query_row['postcode'];
            
    $home $query_row['homeph'];
            
    $mobile $query_row['mobile'];
            
    $twitter $query_row['twitter'];
            
    $facebook $query_row['facebook'];
            
    $website $query_row['website'];
            
    $description $query_row['profiledescription'];
            
    $image $query_row["profileimage"];
            
    $imageName $query_row['profilename'];
            
      }
      }
    // will echo all varibles
    <form action="update.pgp" method="POST"<input type="file" name="imageprofile" class="showimage">
    <
    img src="get.php?id=<php? echo $id ?>">
    <
    input type="submit" name="submit"></form>
    ?>
    This is what I get as a result


    Part 3: When the submit button is clicked, it goes to update.php where I have a SQL quiery updating the data in the back end then directing back to this page. All working fine, everything updates

    Part 4 Get.php
    This is where my code if for the image
    PHP Code:
    <?php
    session_start
    ();
    $con mysql_connect("localhost","root","");
    mysql_select_db("realestate"$con);
    $id=$_GET['userid'];
    $query "SELECT * FROM individual_profile WHERE id = '$id'";

    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
                    
    $id $query_row['id'];
            
    $image $query_row["profileimage"];
            
    $imageName $query_row['profilename'];
            echo 
    $image;
      }
      }
    header("content-type: image/jpeg");
    ?>
    The issue I think I have is when i'm trying to pull the image using which is on edit_profile.php
    Code:
    <img src="get.php?id=<?php echo $id ?>">
    I'm pretty sure the issue is getting the ID to identified the image from my DB.

    Any help would be appreciated
    Last edited by rexhvn; 04-12-2014 at 11:52 AM.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    How are you storing the image on your website?

    Are you storing the image in a directory, and the filename in the database table?

    or

    Are you storing the image in the database table (BLOB - Binary Large Object)?


    Also ... what is this part you're showing?

    <form action="update.pgp" method="POST"<input type="file" name="imageprofile" class="showimage">
    <img src="get.php?id=<php? echo $id ?>">
    <input type="submit" name="submit"></form>


    Is that some text that was stuck in your post (not part of the script)?

    I can't tell if you scripted that or just cut/pasted it wrong in your post.

    That's one of the problems with showing us partial scripts.
    Last edited by mlseim; 04-13-2014 at 03:54 AM.

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    How are you storing the image on your website?

    Are you storing the image in a directory, and the filename in the database table?

    or

    Are you storing the image in the database table (BLOB - Binary Large Object)?


    Also ... what is this part you're showing?

    <form action="update.pgp" method="POST"<input type="file" name="imageprofile" class="showimage">
    <img src="get.php?id=<php? echo $id ?>">
    <input type="submit" name="submit"></form>


    Is that some text that was stuck in your post (not part of the script)?

    I can't tell if you scripted that or just cut/pasted it wrong in your post.

    That's one of the problems with showing us partial scripts.
    Sorry, I am storing the data in the DB as a BLOB

    The above code (now below) was put as part of the script showing how I am attempting to display the image.

    Code:
    <form method="GET" action="#"><img src="get.php?id=<?php echo $id?>">

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by rexhvn View Post
    PHP Code:

            
    echo $image;
      }
      }
    header("content-type: image/jpeg");
    ?> 
    Headers ALWAYS come before the actual content. In this case you're echoing $image and then sending a header. That won't work and is probably the reason you get the correctly sized image place holder but no actual image.
    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!

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    There's another thing then ....

    Your <form> tag needs: enctype="multipart/form-data"

    So it's more like this:

    <form action="update.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="imageprofile" class="showimage">
    <img src="get.php?id=<?php echo $id; ?>">
    <input type="hidden" name="id" value="<?php echo $id;?>">
    <input type="submit" name="submit"></form>

    You'll be sending (using $_POST, not $_GET) the value of "id" to your script called "update.php" using a hidden form tag. You'll need to know that in your other script so you can update the correct profile image.

  • #8
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    There's another thing then ....

    Your <form> tag needs: enctype="multipart/form-data"

    So it's more like this:

    <form action="update.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="imageprofile" class="showimage">
    <img src="get.php?id=<?php echo $id; ?>">
    <input type="hidden" name="id" value="<?php echo $id;?>">
    <input type="submit" name="submit"></form>

    You'll be sending (using $_POST, not $_GET) the value of "id" to your script called "update.php" using a hidden form tag. You'll need to know that in your other script so you can update the correct profile image.
    Thank you. I have made these exact changes but unfortunately still not displaying the image. In terms of updating the data, that works fine and does so by ID, however I am just not able to display the image.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Repost the script ... so we can see it now after all of the changes.

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by rexhvn View Post
    I have made these exact changes but unfortunately still not displaying the image. In terms of updating the data, that works fine and does so by ID, however I am just not able to display the image.
    Did you even notice my little direct and to the point comment?

    You cannot output a header AFTER the image. Headers go first, html / other content after. You clearly have error reporting turned off to else you'd be seeing an error message telling that the headers have already been sent.

    This is a fundamental principle of http. Headers go first (including any call to the header() function) and then everything else afterwards. Thats the way webservers serve their content and it's how the browser expects to receive it. If you break this then you see unwanted effects - as you are.
    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!

  • #11
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Did you even notice my little direct and to the point comment?

    You cannot output a header AFTER the image. Headers go first, html / other content after. You clearly have error reporting turned off to else you'd be seeing an error message telling that the headers have already been sent.

    This is a fundamental principle of http. Headers go first (including any call to the header() function) and then everything else afterwards. Thats the way webservers serve their content and it's how the browser expects to receive it. If you break this then you see unwanted effects - as you are.
    Sorry I didn't see your post. I did place the header above the code in multiple places but still no success. I will repost my code.

  • #12
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    below is all my code for edit_profile.php. This is where the information is displayed and updated.
    _______________________________

    Information being echo'd and sent when submitted - updating the details in the database working fine.

    PHP Code:
    if(isset($_SESSION['username']))
    {
    $query "SELECT * FROM individual_profile WHERE id = '$id'";
    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
            
    $id $query_row['id'];
            
    $fname $query_row['fname'];
            
    $lname $query_row['lname'];
            
    $email $query_row['email'];
            
    $no $query_row['no'];
            
    $street $query_row['street'];
            
    $suburb $query_row['suburb'];
            
    $postcode $query_row['postcode'];
            
    $home $query_row['homeph'];
            
    $mobile $query_row['mobile'];
            
    $twitter $query_row['twitter'];
            
    $facebook $query_row['facebook'];
            
    $website $query_row['website'];
            
    $description $query_row['profiledescription'];
            
    $image $query_row["profileimage"];
            
    $imageName $query_row['profilename'];

    // HTML inputs - Name, email etc..
            
      
    }
      }
    }
    ?> 
    Code:
    <form method="POST" enctype="multipart/form-data">
    <input type="file" name="imageprofile">
    <img src="get.php?id=<?php echo $id ?>" class="showimage" />
    <input type="hidden" name="id" value="<?php echo $id;?>">
    <input type="submit" name="upload"></form>
    PHP Code:
    ?php
    if(isset($_POST['upload']))
    {  

    set_time_limit(3000); 
    $con mysql_connect("localhost","root","");
    mysql_select_db("realestate"$con);

    $id=$_SESSION['id'];
    $profilename mysql_real_escape_string($_FILES['profileimage']['name']);
    $profiledata mysql_real_escape_string(file_get_contents($_FILES['profileimage']['tmp_name']));
    $profiletype mysql_real_escape_string($_FILES['profileimage']['type']);


    $query2="UPDATE individual_profile SET profilename='$profilename', profileimage='$profiledata' WHERE id='$id'";



    if (!
    mysql_query($query2,$con))
      {
      die(
    'Error: ' mysql_error());
      }
      else
      echo 
    '<span class="updated"><img src="/Main content/property admin/tick.jpg" height="50" width="53" class="tick"/>
    <center>Your details have been updated successfully!</center></span>'
    ;
    mysql_close($con);
    }
    ?> 
    ________________________________________________
    Getting the image - get.php - this is where I am trying to get the details to display the image

    PHP Code:
    <?php
    session_start
    ();
    $con mysql_connect("localhost","root","");
    mysql_select_db("realestate"$con);

    $query "SELECT * FROM individual_profile WHERE id = '$id'";
            

    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
            
    $id $query_row['id'];
            
    $image $query_row["profileimage"];
            
    $imageName $query_row['profilename'];
            
    header("content-type: image/jpeg"); // tried in many locations
            
    echo $image;
      }
    }


    if (!
    mysql_query($query,$con))
      {
      die(
    'Error: ' mysql_error());
      }
    mysql_close($con);


    ?>
    All help is greatly appreciated.
    Last edited by rexhvn; 04-29-2014 at 02:02 PM.

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Try this ....

    echo '<img src="data:image/jpeg;base64,'.base64_encode( $query_row["profileimage"] ).'"/>';

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Ok give this last suggestion from mlseim a go and if it's still not working then I can take a look via teamviewer for you if you like.

    Also if you use firefox, download the HttpFox plugin and examine the headers for the image request to see if the correct headers and file content are being output. You may find that it contains error messages 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!

  • #15
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Try this ....

    echo '<img src="data:image/jpeg;base64,'.base64_encode( $query_row["profileimage"] ).'"/>';
    Thank you, this worked!


  •  

    Posting Permissions

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