Associating photo with its caption details in a database

Hellos!..

I have this form

<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.

Save the picture name in the database, or give the picture the picID as name.

thank you Guido…

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

<img src="" />

But I’m sure you know that, and it’s just me that doesn’t understand the question.

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.

&lt;?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 '&lt;img src="' . $path_to . $image_folder . $file . '" alt="caption" border="0" /&gt;&lt;br /&gt;' . $caption;
} else {
 echo 'I have no pic, I must be ugly!';
}
?&gt;

@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.

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).

For displaying all users example:


&lt;?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 '&lt;img src="' . $path_to . $image_folder . $file . '" alt="caption" border="0" /&gt;&lt;br /&gt;' . $caption;
 }
} else {
 echo 'I have no pic, I must be ugly!';
}
?&gt;

Function for grabbing for just one user example:


&lt;?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 '&lt;img src="' . $path_to . $image_folder . $file . '" alt="caption" border="0" /&gt;&lt;br /&gt;' . $caption;
  } else {
   return 'No pic';
  }
?&gt;

* don’t use the above code verbatim, are just examples.

Oh!..I real don’t understand DB lookup thing, I have check on the net but I have not understood a thing!.:nono:
I am a beginner in PHP can you help me how its done1:scratch:

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.

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:

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 …

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?

Easiest way, if your user is logged in:


<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:


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/how-to-kill-mysql-performance

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.

     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.

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.

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.

Off Topic:

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.