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 19
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    displaying an image from a MYSql database.

    Hi
    I have a small MYSQL database that I'm trying to display an image. I modified some info I found online. It appears that the image is being inserted in the database - it's just my PHP to display it that's gone wrong.
    I have a sign_up.html, sign_up.php, display_users.php, connect.php and image.php (which is where I think the error is). When I run display_users.php, it shows a placeholder for the image.

    I've included some of the files - I'd appreciate any advice.

    Also, while I started this project with the intention of saving the image as a BLOB, the info I found saved it as LONG TEXT. why is that?

    PHP Code:
    ============================================
    sign_up.html
    ============================================
    <
    form action="sign_up.php" method="POST" enctype="multipart/form-data">


    <
    br />Please enter your preferred username: <br />
    <
    input type="text" name="username" size="25" maxlength="50" value=""><br /><br />
    Please enter first name: <br />
    <
    input type="text" name="f_name" size="25" maxlength="55" value=""><br /><br />
    Please enter last name: <br />
    <
    input type="text" name="l_name" size="25" maxlength="55" value=""><br /><br />
    Please enter location of your favicon: <br />
    <
    input type="file" name="favicon" accept="image/jpeg">
    <
    input name="MAX_FILE_SIZE" value="102400" type="hidden">

    <
    br />


    <
    input type="submit" value="Submit Form">
    </
    form>
    ============================================
    sign_up.php
    ============================================
    require(
    "connect.php");

    //Post Variables
    $username mysql_real_escape_string($_POST['username']);
    $first_namemysql_real_escape_string($_POST['f_name']);
    $last_name mysql_real_escape_string($_POST['l_name']);
    $favicon mysql_real_escape_string($_POST['favicon']);
    $id mysql_real_escape_string($_POST['id']);

    $image basename$_FILES['favicon']['name']); 
    $sizeimage=$_FILES['favicon']['size'];
            if(
    move_uploaded_file($_FILES['favicon']['tmp_name'], $image)) 
            {
                
    $handle fopen($image,'r');
                
    $file_content fread($handle,$_FILES['favicon']['size']);
                
    fclose($handle);
                
    $encodedimg chunk_split(base64_encode($file_content)); 
     
    //Update Table
    mysql_query("INSERT 
    INTO user 
        ( id,
        username
       , first_name
       , last_name
       , favicon
       ) 
    VALUES 
      ( '$id',
      '$username'
       , '$first_name'
       , '$last_name'
       , '$encodedimg'
      ) ;
    "
    );
     
    unlink($image);
                
    $success=1;
            }
            else
            {
                
    $reason.= "<p>There was an error uploading the file, please try again!</p>";
            }
    ============================================
      
    display_users.php
    ============================================
    require (
    "connect.php");

    // select our database
            
    mysql_select_db("basil_bookmark") or die(mysql_error());

    //Display Table

    if (!$result)
     {
     die(
    '<p>Error performing query: ' mysql_error() . '</p>');
     }

    echo 
    "<table border='1' width='80%'cellpadding='3' cellspacing='0'>";
    echo 
    "<tr>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Username</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>First Name</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Last Name</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Favicon</strong></td>";

    echo 
    "</tr>";


    while (
    $row mysql_fetch_array($result)) {

    echo 
    "<tr>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["username"] ."</strong></td>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["first_name"] ."</strong></td>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["last_name"] ."</strong></td>";

    echo 
    "<td bgcolor='#FFccff' align='center'><strong><img src='http://basil.x10.mx/image.php?id='".$row['id']. "alt='favicon'></img></strong></td>";
    echo 
    "</tr>";
    ============================================
    image.php
    ============================================
    include(
    'connect.php');
        
    $id$_REQUEST["id"];    
        
    $q "SELECT ('favicon') FROM 'user' where id=".$id;
        
    $result mysql_query($q);        
        while (
    $row mysql_fetch_array($result) ) { 
        
    $img $row['favicon'];
        }
        echo 
    base64_decode($img);

    Last edited by basil60; 12-23-2010 at 01:40 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    I think you need to have the content-type defined.
    In your case, jpeg ... instead of x-icon,gif, or png?
    You say "favicon", but you aren't using .ico ?

    PHP Code:
    image.php
    ============================================
    include(
    'connect.php');
        
    $id$_REQUEST["id"];    
        
    $q "SELECT ('favicon') FROM 'user' where id=".$id;
        
    $result mysql_query($q);        
        while (
    $row mysql_fetch_array($result) ) { 
        
    $img $row['favicon'];
        }

    header('Content-Type: image/jpeg');
    // I'm not sure which one of these to use...
    echo base64_decode($img);
    //echo $img;

    Last edited by mlseim; 12-23-2010 at 02:48 AM.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks. Favicon probably should have been avatar - but I guess that's irrelevant.

    The example I used - here - didn't need the datatype - and I verified this on my own system.

    That is, unless my interpretation has gone awry.

    *Using FF - I can see for the image placeholders, the path is - http://basil.x10.mx/bookmark/image.php?id=
    That suggest to me that image.php isn't successfully appending the image id to each image. But I can't see why!
    Last edited by basil60; 12-23-2010 at 03:27 AM.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    I think I know why ...

    This line:
    $id= $_REQUEST["id"];

    There is a risk to using $_REQUEST.
    $_REQUEST has changed a bit during the last PHP revisions.
    I'll bet that with your PHP version, in the config, it has been disabled along with register_global (as a default).

    Use this instead ...
    $id= $_GET["id"];

    ============================

    If you are using a variable with a URL and you also sometimes use it with a <form method="post>,
    you have to determine whether the variable is GET or POST. In the past, you could have used
    REQUEST to cover either of them.

    If $_REQUEST is disabled for you ...

    PHP Code:
    <?php

    // The "old way" of doing it...
    // covers both GET and POST ...
    $action=$_REQUEST['action'];

    // If REQUEST has been disabled, check for either one ...
    if(isset($_GET['action']){
    $action=$_GET['action'];
    }
    if(isset(
    $_POST['action']){
    $action=$_POST['action'];
    }

    ?>


    .
    Last edited by mlseim; 12-23-2010 at 01:29 PM.

  • #5
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you. I changed that, but without success. The example I used from the web uses _REQUEST, and that tutorial does work.

    It's only when I try to adapt it into my own implementation that it fails - operator error!!!!!

    Something I've done is preventing the id being appended to the <img src ..> tag, giving me
    Code:
    http://basil.x10.mx/bookmark/image.php?id=
    .

    I tried comparing tags from mine to the tutorial, without success. The only other thing I can see that could be is the way I tried to concatenate the elements.
    Code:
    echo "<td bgcolor='#FFccff' align='center'><strong><img src='http://basil.x10.mx/bookmark/image.php?id='".$row['id']. "alt='favicon'></img></strong></td>";
    Any other advice might help to eliminate more grey hairs!!!

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    aha ... I see the problem!

    Missing the single quote ... see the red single quote ...


    echo "<td bgcolor='#FFccff' align='center'><strong><img src='http://basil.x10.mx/bookmark/image.php?id='".$row['id']."' alt='favicon'></img></strong></td>";


    alternative method:

    echo "<td bgcolor='#FFccff' align='center'><strong><img src='http://basil.x10.mx/bookmark/image.php?id='{$row['id']}' alt='favicon'></img></strong></td>";


    .

  • #7
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy

    Thanks for taking the time.
    When I use either of the methods you've suggested, I see the word favicon only (I'm guessing the alt tag).
    If I leave it the way I had it, it's a broken image. There must be some other operator error hidden away.

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    oops ....

    I still had it wrong ... try this ....

    echo "<td bgcolor='#FFccff' align='center'><img src='http://basil.x10.mx/bookmark/image.php?id=".$row['id']."' alt='favicon' /></td>";

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Why not make it easier to understand and put the PHP variable inside the string using these brackets {} like this.

    PHP Code:
    echo "<td bgcolor='#FFccff' align='center'><img src='".
         
    "http://basil.x10.mx/bookmark/image.php?id={$row['id']}' alt='favicon' /></td>"
    You can also use double quotes with a backslash like so
    PHP Code:
    echo "<td bgcolor=\"#FFccff\" align=\"center\"><img src=\"".
         
    "http://basil.x10.mx/bookmark/image.php?id={$row['id']}\" alt=\"favicon\" /></td>"
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #10
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks - but again, if I use either "bits" of code supplied, it shows the word (alt tag?) favicon, rather than an image or even broken image.

    I'm reposting my code, in case I've accidentally altered something. I've been through, and double checked what I've done, but I could easily be overlooking some glaring error.

    PHP Code:
    =====================================================
    image.php
    <?php
    include('connect.php');
        
    $id$_GET['id'];    
        
    $q "SELECT ('favicon') FROM 'user' where id = " $id;
        
    $result mysql_query($q);        
        while (
    $row mysql_fetch_array($result) ) { 
        
    $img $row['favicon'];
        }
        echo 
    base64_decode($img); 
    =========================================================
    display_users.php

    require ("connect.php");

    // select our database
            
    mysql_select_db("basil_bookmark") or die(mysql_error());
    $result mysql_query("SELECT * FROM user");
    //Display Table

    if (!$result)
     {
     die(
    '<p>Error performing query: ' mysql_error() . '</p>');
     }

    echo 
    "<table border='1' width='80%'cellpadding='3' cellspacing='0'>";
    echo 
    "<tr>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Username</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>First Name</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Last Name</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Favicon</strong></td>";

    echo 
    "</tr>";


    while (
    $row mysql_fetch_array($result)) {

    echo 
    "<tr>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["username"] ."</strong></td>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["first_name"] ."</strong></td>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["last_name"] ."</strong></td>";

    echo 
    "<td bgcolor='#FFccff' align='center'><img src='http://basil.x10.mx/bookmark/image.php?id=".$row['id']."' alt='favicon' /></td>";//echo "<td bgcolor='#FFccff' align='center'><strong>". $content ."</strong></td>";
    echo "</tr>";

    }    

     echo (
    "\n</table>\n");
     print 
    "There are " mysql_num_rows($result) . " rows in the Bookmarks database\n";

    ?> 


    ?>

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    When I go to this link by itself ...

    http://basil.x10.mx/bookmark/image.php?id=2

    I get a query failure on line 6 ...

    Line 6 is this:
    $q = "SELECT ('favicon') FROM 'user' where id = " . $id;

    Try this:

    $q = "SELECT favicon FROM user where id = '$id' ";



    .

  • #12
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks.

    When I alter that, and call the line as you did, I see a whole bunch of code. (http://basil.x10.mx/bookmark/image.php?id=9)
    If I then call the display_users.php file, it appends a number to the image - ie Src=http://basil.x10.mx/bookmark/image.php?id=9, but still you can only see the alt tag.

    I think that narrows down the problem. To insert the images into the database, I used:
    PHP Code:
    sign_up.php
    =========================================================
    <?
    require("connect.php");

    //Post Variables
    $username mysql_real_escape_string($_POST['username']);
    $first_namemysql_real_escape_string($_POST['f_name']);
    $last_name mysql_real_escape_string($_POST['l_name']);
    $favicon mysql_real_escape_string($_POST['favicon']);
    $id mysql_real_escape_string($_POST['id']);

    $image basename$_FILES['favicon']['name']); 
    $sizeimage=$_FILES['favicon']['size'];
            if(
    move_uploaded_file($_FILES['favicon']['tmp_name'], $image)) 
            {
                
    $handle fopen($image,'r');
                
    $file_content fread($handle,$_FILES['favicon']['size']);
                
    fclose($handle);
                
    $encodedimg chunk_split(base64_encode($file_content)); 
     
    //Update Table
    mysql_query("INSERT 
    INTO user 
        ( id,
        username
       , first_name
       , last_name
       , favicon
       ) 
    VALUES 
      ( '$id',
      '$username'
       , '$first_name'
       , '$last_name'
       , '$encodedimg'
      ) ;
    "
    );
     
    unlink($image);
                
    $success=1;
            }
            else
            {
                
    $reason "<p>There was an error uploading the file, please try again!</p>";
            }
      
    print(
    "Thank you <br />Your signup is complete.");
    print (
    "<p><a href='http://basil.x10.mx/bookmark/'>Home</a></p>");
    // Close our MySQL Link

    ?>
    The info I found on the web, did it this way, and I tried to modify it.
    The oriiginal insert looked like this.
    PHP Code:
    <?php
    include('config.php');
    $image basename$_FILES['imagefile']['name']);
    $sizeimage=$_FILES['imagefile']['size'];
    if(
    move_uploaded_file($_FILES['imagefile']['tmp_name'], $image))
    {
    $handle fopen($image,'r');
    $file_content fread($handle,$_FILES['imagefile']['size']);
    fclose($handle);
    $encodedimg chunk_split(base64_encode($file_content));
    $sql "INSERT INTO imagesdata SET image='$encodedimg'";
    mysql_query($sql);
    unlink($image);
    $success=1;
    }
    else
    {
    $reason.= "<p>There was an error uploading the file, please try again!</p>";
    }
    ?>
    <html>
    <body>
    <? if($success==1) {?>
    <p>Upload success.</p>
    <a href="javascript:history. go(-1)">Back</a>
    <? }
    else {echo 
    $reason;}?>
    </body>
    </html>

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    How about if you forget about the "base64_encode" stuff you're using ...

    In examples I've seen with BLOB, that isn't used.

    Like this example ...
    http://www.anyexample.com/programmin...b_storage).xml

    Like you mentioned earlier ... your problem is most likely in how you defined the column.
    It has to be defined as a BLOB of some kind... in that example it's:
    data` mediumblob NOT NULL,

  • #14
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you. I did work through the thread you gave me. But couldn't apply it to my scenario. I suppose I could have just done a copy and paste, but I'm not sure what I would have learnt. Trawled the web and went back to this

    And I think I'm a lot closer than I was. When I run display_users.php, I get a whole heap of code now. See here. I'm guessing that's cos of my header stuff, but I'm not sure.
    Here's what my display_users.php looks like now.
    PHP Code:
    <?php
    ini_set 
    ("display_errors""1");
    error_reporting(E_ALL);
    require (
    "connect.php");

    // select our database
            
    mysql_select_db("basil_bookmark") or die(mysql_error());
    $result mysql_query("SELECT * FROM user ORDER BY id DESC");
    //$content = "<img src=".$domain."/image2.php?id=".$row['id']."alt='favicon'></img>";
    //Display Table
    header("Content-type: img/"$ROW['image_type']);
    if (!
    $result)
     {
     die(
    '<p>Error performing query: ' mysql_error() . '</p>');
     }

    echo 
    "<table border='1' width='80%'cellpadding='3' cellspacing='0'>";
    echo 
    "<tr>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Username</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>First Name</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Last Name</strong></td>";
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Favicon</strong></td>";

    echo 
    "</tr>";


    while (
    $row mysql_fetch_array($result)) {

    echo 
    "<tr>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["username"] ."</strong></td>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["first_name"] ."</strong></td>";
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["last_name"] ."</strong></td>";

     
    echo 
    "<td bgcolor='#FFccff' align='center'>".$row["image_data"]. "</td>";
     
    echo 
    "</tr>";

    }    

     echo (
    "\n</table>\n");
     print 
    "There are " mysql_num_rows($result) . " rows in the Bookmarks database\n";

    ?> 
    ========================================================
    and sign_up.php just in case

    require("connect.php");

    //Post Variables
    $username = mysql_real_escape_string($_POST['username']);
    $first_name= mysql_real_escape_string($_POST['f_name']);
    $last_name = mysql_real_escape_string($_POST['l_name']);
    //$image_data = $_POST['file'];

    //read image bytes in to the data variable

    $file = fopen($_FILES['file']['tmp_name'], "r");
    $data = addslashes(fread($file, filesize($_FILES['file']['tmp_name'])));
    fclose($file);

    //Update Table
    mysql_query("INSERT 
    INTO user 
        ( username
       , first_name
       , last_name
       , image_data
       ,image_type
       ) 
    VALUES 
      ( '$username'
       , '$first_name'
       , '$last_name'
       , '$data'
       ,'jpg'
      ) ;
    ");
        
      
    print("Thank you <br />Your signup is complete.");
    print ("<p><a href='http://basil.x10.mx/bookmark/'>Home</a></p>");
    // Close our MySQL Link

    ?>

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    You can't mix header types.
    By echoing text, you've already created a header.

    Try this ... using the <img> tag ...
    Here is the script modified ....
    PHP Code:
    <?php 
    ini_set 
    ("display_errors""1"); 
    error_reporting(E_ALL); 
    require (
    "connect.php"); 

    // select our database 
            
    mysql_select_db("basil_bookmark") or die(mysql_error()); 
    $result mysql_query("SELECT * FROM user ORDER BY id DESC"); 
    //$content = "<img src=".$domain."/image2.php?id=".$row['id']."alt='favicon'></img>"; 
    //Display Table 
    //header("Content-type: img/". $ROW['image_type']); 
    if (!$result
     { 
     die(
    '<p>Error performing query: ' mysql_error() . '</p>'); 
     } 

    echo 
    "<table border='1' width='80%'cellpadding='3' cellspacing='0'>"
    echo 
    "<tr>"
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Username</strong></td>"
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>First Name</strong></td>"
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Last Name</strong></td>"
    echo 
    "<td align='center' bgcolor='#FF3399'><strong>Favicon</strong></td>"

    echo 
    "</tr>"


    while (
    $row mysql_fetch_array($result)) { 

    echo 
    "<tr>"
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["username"] ."</strong></td>"
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["first_name"] ."</strong></td>"
    echo 
    "<td bgcolor='#FFccff' align='center'><strong>"$row["last_name"] ."</strong></td>"

      
    //echo "<td bgcolor='#FFccff' align='center'>".$row["image_data"]. "</td>"; 

    echo "<td bgcolor='#FFccff' align='center'><img src='{$row['image_data']}' /></td>"


      
    echo 
    "</tr>"

    }     

     echo (
    "\n</table>\n"); 
     print 
    "There are " mysql_num_rows($result) . " rows in the Bookmarks database\n"

    ?>  
    ======================================================== 
    and sign_up.php just in case 

    require("connect.php"); 

    //Post Variables 
    $username = mysql_real_escape_string($_POST['username']); 
    $first_name= mysql_real_escape_string($_POST['f_name']); 
    $last_name = mysql_real_escape_string($_POST['l_name']); 
    //$image_data = $_POST['file']; 

    //read image bytes in to the data variable 

    $file = fopen($_FILES['file']['tmp_name'], "r"); 
    $data = addslashes(fread($file, filesize($_FILES['file']['tmp_name']))); 
    fclose($file); 

    //Update Table 
    mysql_query("INSERT  
    INTO user  
        ( username 
       , first_name 
       , last_name 
       , image_data 
       ,image_type 
       )  
    VALUES  
      ( '$username' 
       , '$first_name' 
       , '$last_name' 
       , '$data' 
       ,'jpg' 
      ) ; 
    "); 
         
       
    print("Thank you <br />Your signup is complete."); 
    print ("<p><a href='http://basil.x10.mx/bookmark/'>Home</a></p>"); 
    // Close our MySQL Link 

    ?>

    If that still doesn't work, I have one more thing to try.



    .
    Last edited by mlseim; 12-29-2010 at 01:51 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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