SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    associating photo with its caption details in a database

    Hellos!..

    I have this form
    Code HTML4Strict:
    <form action="" enctype="multpart/form-data" method="post">
            <label for="username">username</label>
            <input type="text" name="username" id="username" /><br/>
            <label for="image">image</label>
     
            <input type="file" name="image" id="image" /><br/>
            <input type="submit" name="sendinfo" id="sendinfo" value="sendinfo"/>
        </form>
    which is supposedly be adding username into a database along with their user profile picture. I have two tables User(useid,username) profilepic(picID,userid,caption)...I have set the profile picture to be added in the folder out of my site folder(www)..Can someone help me how to be able to associate the picture in the folder with the caption in the database.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Save the picture name in the database, or give the picture the picID as name.

  3. #3
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you Guido..

    that works, but now how do I display back to the browser the stored images.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    HTML Code:
    <img src="" />
    But I'm sure you know that, and it's just me that doesn't understand the question.

  5. #5
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Store the filename in profilepic as well. Then ask the database for the user's picture like this:

    Also why not store the pic info in the user table to avoid having to use JOINs and extra queries? JOINs suck balls.

    PHP Code:
    <?php
    $path_to 
    'http://mysite.com/'// with trailing  slash
    $image_folder 'profilepics/'// with trailing  slash
    $sql mysql_query('SELECT `file`,`caption` FROM `profilepic` WHERE `userid="' . (int)$userid '" LIMIT 1') or die(mysql_error());
    if (
    mysql_num_rows($sql) != 0) {
     
    $r mysql_fetch_row($sql);
     
    $file stripslashes($r[0]);
     
    $caption stripslashes($r[1]);
     echo 
    '<img src="' $path_to $image_folder $file '" alt="caption" border="0" /><br />' $caption;
    } else {
     echo 
    'I have no pic, I must be ugly!';
    }
    ?>

  6. #6
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Guido..what I meant is to be able to know this picture belong to this user. As the picture is in the folder and user is stored in the database.

    @ LinuxFreelancer thank you, that is a good idea I think I will use that. The problem now is to know this image belong to this user as there are going to be many pictures in the folder. I have tried the code, Now all users are displayed with the same image.

  7. #7
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A userID needs to be added to the form (first post) or do a DB lookup on submit (post 5 - change $userid to username = $_POST['username'] in the query).

  8. #8
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nkacharani View Post
    @ LinuxFreelancer thank you, that is a good idea I think I will use that. The problem now is to know this image belong to this user as there are going to be many pictures in the folder. I have tried the code, Now all users are displayed with the same image.
    For displaying all users example:
    PHP Code:
    <?php
    $path_to 
    'http://mysite.com/'// with trailing  slash
    $image_folder 'profilepics/'// with trailing  slash
    $sql mysql_query('SELECT `file`,`caption` FROM `profilepic`') or die(mysql_error());
    if (
    mysql_num_rows($sql) != 0) {
     while(
    $r mysql_fetch_row($sql)) {
      
    $file stripslashes($r[0]);
      
    $caption stripslashes($r[1]);
      echo 
    '<img src="' $path_to $image_folder $file '" alt="caption" border="0" /><br />' $caption;
     }
    } else {
     echo 
    'I have no pic, I must be ugly!';
    }
    ?>
    Function for grabbing for just one user example:
    PHP Code:
    <?php
    if (!function_exists('my_getPic')) {
     function 
    my_getPic($userid) {
      
    $path_to 'http://mysite.com/'// with trailing  slash
      
    $image_folder 'profilepics/'// with trailing  slash  
      
    $sql mysql_query('SELECT `file`,`caption` FROM `profilepic` WHERE `userid="' . (int)$userid '" LIMIT 1') or die(mysql_error());
      if (
    mysql_num_rows($sql) != 0) {
       
    $r mysql_fetch_row($sql);
       
    $file stripslashes($r[0]);
       
    $caption stripslashes($r[1]);
       return 
    '<img src="' $path_to $image_folder $file '" alt="caption" border="0" /><br />' $caption;
      } else {
       return 
    'No pic';
      }
    ?>
    * don't use the above code verbatim, are just examples.

  9. #9
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    A userID needs to be added to the form (first post) or do a DB lookup on submit (post 5 - change $userid to username = $_POST['username'] in the query).
    Oh!..I real don't understand DB lookup thing, I have check on the net but I have not understood a thing!.
    I am a beginner in PHP can you help me how its done1
    Last edited by nkacharani; Jun 20, 2011 at 16:03. Reason: typo error

  10. #10
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't have it straight by tomorrow, email the script that has the issue and I will fix that part for you. (make sure to put in a zip file) My email is:

    {jeff at crackfeed dot com}

    If I dont get out of here and hang with my wife tonight I will be singing in a very high pitch, if you know what I mean.

  11. #11
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nkacharani View Post
    Oh!..I real don't understand DB lookup thing, I have check on the net but I have not understood a thing!.
    I am a beginner in PHP can you help me how its done1

    Good in theory but brings up security issues, as well as performance.

  12. #12
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LinuxFreelancer View Post
    Good in theory but brings up security issues, as well as performance.
    How so? I just suggested he change your userid = $userid to lookup the user name since his form didn't provide a userid. This could have been done with a join with doesn't hinder performance at all.

    Just a guess:
    Code:
    SELECT p.file, p.caption
    FROM profilepic as p
    OUTER JOIN User as u
    	ON p.userid = u.userid
    WHERE u.username = $_POST['username']
    LIMIT 1

  13. #13
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    How so? I just suggested he change your userid = $userid to lookup the user name since his form didn't provide a userid. This could have been done with a join with doesn't hinder performance at all.

    Just a guess:
    Code:
    SELECT p.file, p.caption
    FROM profilepic as p
    OUTER JOIN User as u
        ON p.userid = u.userid
    WHERE u.username = $_POST['username']
    LIMIT 1
    A JOIN always hinders performance. Joins sucks balls, even with an INDEX setup.

    So you see no chance of a slow down if one page makes this call 20 times? that is 20 queries in one pageload for the same thing but different users.

    Security.... for starters ...

    Code:
    SELECT p.file, p.caption
    FROM profilepic as p
    OUTER JOIN User as u
        ON p.userid = u.userid
    WHERE u.username = $_POST['username']
    LIMIT 1
    [/QUOTE]

    You just send user posted data directly to a query and got your a** hacked and you have to give refunds to all of your site members and you just go sued because little 13 year old Amy Joe's private into just got abused by hackers after they got it from your database. Any more silly questions?

  14. #14
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nkacharani View Post
    Oh!..I real don't understand DB lookup thing, I have check on the net but I have not understood a thing!.
    I am a beginner in PHP can you help me how its done1
    Easiest way, if your user is logged in:

    Code HTML4Strict:
    <form action="" enctype="multpart/form-data" method="post">
    <!-- removed user name - user is already logged in right? -->
        <label for="image">image</label>
        <input type="file" name="image" id="image" /><br/>
     
    <!-- send the userid value to the post array (you probably already have this info) -->
    	<input type="hidden" name="userid" value="1" />
        <input type="submit" name="sendinfo" id="sendinfo" value="sendinfo"/>
    </form>

    Then using LinuxFreelancer's code:
    Code:
    echo my_getPic( $_POST['userid'] );
    The lookup was as I noted in the above post with the SQL join, again, this is easier and can use LinuxFreelancer's code

  15. #15
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    Easiest way, if your user is logged in:

    Code HTML4Strict:
    <form action="" enctype="multpart/form-data" method="post">
    <!-- removed user name - user is already logged in right? -->
    <label for="image">image</label>
    <input type="file" name="image" id="image" /><br/>
     
    <!-- send the userid value to the post array (you probably already have this info) -->
    <input type="hidden" name="userid" value="1" />
    <input type="submit" name="sendinfo" id="sendinfo" value="sendinfo"/>
    </form>

    Then using LinuxFreelancer's code:
    Code:
    echo my_getPic( $_POST['userid'] );
    The lookup was as I noted in the above post with the SQL join, again, this is easier and can use LinuxFreelancer's code
    Do not use a JOIN for this. This person is dead wrong. New coders need to learn that JOINs should be rarely used unless you are very experienced with MySQL.

    I hate to be rude, but that is really not a good idea at all. Combine the 2 databases, it won't make that big a difference. However, joining two tables to do the same job, you will feel the difference one you have some users online.

    Please watch this: http://www.slideshare.net/techdude/h...ql-performance

  16. #16
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LinuxFreelancer View Post
    A JOIN always hinders performance. Joins sucks balls, even with an INDEX setup.

    So you see no chance of a slow down if one page makes this call 20 times? that is 20 queries in one pageload for the same thing but different users.

    Security.... for starters ...

    Code:
    SELECT p.file, p.caption
    FROM profilepic as p
    OUTER JOIN User as u
        ON p.userid = u.userid
    WHERE u.username = $_POST['username']
    LIMIT 1
    You just send user posted data directly to a query and got your a** hacked and you have to give refunds to all of your site members and you just go sued because little 13 year old Amy Joe's private into just got abused by hackers after they got it from your database. Any more silly questions?
    I take it this would be a single call for a user and I gave a suggestion that wouldn't require this.

    Also you noted the security issue before I posted any code, so what is the security reason? This? I surely hope the OP is sanitizing any data from the user before using it. Also, the code I posted was off the top of my head to show an example to you and the OP without changing his form (if he could). As stated, If he could, then I already posted what he should do.

  17. #17
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    I take it this would be a single call for a user and I gave a suggestion that wouldn't require this.

    Also you noted the security issue before I posted any code, so what is the security reason? This? I surely hope the OP is sanitizing any data from the user before using it. Also, the code I posted was off the top of my head to show an example to you and the OP without changing his form (if he could). As stated, If he could, then I already posted what he should do.
    Ok now you are just wasting space, we are way past the initial issue and are on another issue now...

    @ LinuxFreelancer thank you, that is a good idea I think I will use that. The problem now is to know this image belong to this user as there are going to be many pictures in the folder. I have tried the code, Now all users are displayed with the same image.
    I need to se the code, can't spend time guessing because this could be due to multiple causes in the area you are extracting the data at.

    Do not use this, this is an example to show how one of my scripts does this for a singlular user. Notice that the I have a separate folder for each users' pics? Not needed but keeps things clean.

    PHP Code:
         if ($pic == 'default.jpg' || $pic == null) {
          
    $profilePic '<img src="'.$path_to 'UserFiles/Photos/defaultavatar.jpg" alt="'.$profileUser.'">';
         } else {
          
    $profilePic '<img src="'.$path_to 'UserFiles/Photos/' $profileId '/'.$pic.'" alt="'.$profileUser.'">';
         } 
    A unique name needs assigned to every image. on upload, rename the image as you upload it and then store the name of the iamge in teh `file` column. Really no way to mess that up.

  18. #18
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LinuxFreelancer View Post
    Do not use a JOIN for this. This person is dead wrong. New coders need to learn that JOINs should be rarely used unless you are very experienced with MySQL.

    I hate to be rude, but that is really not a good idea at all. Combine the 2 databases, it won't make that big a difference. However, joining two tables to do the same job, you will feel the difference one you have some users online.

    Please watch this: How to Kill Mysql Performance
    The code you quoted from me was to be used without a join. I added the userid to a hidden input so he could use the code you posted.

  19. #19
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I stand corrected, I misread this:

    The lookup was as I noted in the above post with the SQL join, again, this is easier and can use LinuxFreelancer's code
    Sorry CE.

  20. #20
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Off Topic:

    Quote Originally Posted by LinuxFreelancer View Post
    New coders need to learn that JOINs should be rarely used unless you are very experienced with MySQL.
    Joins should be used when needed.
    And running the same query 20 times sounds like a query in a loop, which is always a bad idea (and has nothing to do with joins).

    In this case, if all users have at the most one pic, LF's tip to use one table instead of two is correct. If you want to store more pics for each user, a second table is needed and a join is inevitable.

  21. #21
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So the function was for running once. This was for polling for multiple users:

    PHP Code:
     <?php
    $path_to 
    'http://mysite.com/'// with trailing  slash
    $image_folder 'profilepics/'// with trailing  slash
    $sql mysql_query('SELECT `file`,`caption` FROM `profilepic`') or die(mysql_error());
    if (
    mysql_num_rows($sql) != 0) {
     while(
    $r mysql_fetch_row($sql)) {
      
    $file stripslashes($r[0]);
      
    $caption stripslashes($r[1]);
      echo 
    '<img src="' $path_to $image_folder $file '" alt="caption" border="0" /><br />' $caption;
     }
    } else {
     echo 
    'I have no pic, I must be ugly!';
    }
    ?>
    With a well designed database, I have found that joins are rarely needed. I believe it is just one pic per user, I could be wrong.

  22. #22
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you all..I'm working on it!

  23. #23
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while($row mysql_fetch_assoc($show)){
    echo 
    $row['userrname'];
    echo 
    '<img src="images/user1.jpg" />';

    needs to be:


    PHP Code:
    while($row mysql_fetch_assoc($show)){
    echo 
    $row['username'];
    echo 
    '<img src="images/' $row['image'] . '" />';

    I have not had time to read the entire script but this should solve that issue.

  24. #24
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ LF...thanks!..that does the trick


Bookmarks

Posting Permissions

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