PHP and MYSQL image management [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

PHP and MYSQL image management [solved]

Post by scarface222 »

Hey guys just wondering if anyone has any idea how to solve this issue I have been struggling with. I have created a MYSQL table with a username field and 20 image fields (each named 1-20 respectively) so I can store image names when they are uploaded in order to be able to later reference the location of them and limit uploads. I want to create a php code where I can check when a field is empty (after image upload button clicked) and then insert the image name in that field. If the field is not empty then I want the code to move on and insert into the next empty slot until full. I wrote this code but it does not work, it just skips and goes to the end and echos 'image space full'. I tried echoing the $count variable and it says 1. When I changed the 0 value in the code check to 1 as a test, it just gives me the message 'error select query failed'. If anyone has any ideas at all, they are greatly appreciated. Is this even the best way to approach it? It is a lot of code and if I want to add more images it will be more.

PHP code

Code: Select all

 
$filename=mysql_real_escape_string($_FILES['image']['name']);
 
$first_query="SELECT 1 FROM images WHERE username='$username'";
$result=mysql_query($first_query) or die('Error, select query failed');
$count=mysql_num_rows($result);
    
if ($count==[color=#FF0000]0[/color]){
    $third_query = "UPDATE images SET 1='$filename'
WHERE username='$username'";
mysql_query($third_query) or die('Error, insert query failed');
}
else {
    $first_query="SELECT 2 FROM images WHERE username='$username'";
$result=mysql_query($first_query) or die('Error, select query failed');
$count=mysql_num_rows($result);
}
if ($count==[color=#FF0000]0[/color]){
    $third_query = "UPDATE images SET 2='$filename'
WHERE username='$username'";
mysql_query($third_query) or die('Error, insert query failed');
}
 
else{
    echo 'image space full';
    return;
    }
}
?>
code is snippet and continues to check up to 20
Last edited by scarface222 on Wed Apr 08, 2009 12:42 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP and MYSQL check empty values (any ideas appreciated)

Post by requinix »

You... heh... you named your fields with... ha ha... numbers?
:lol:

Normalize your database. Have one table for people, one for pictures. Then there's no wasted space and no fixed limit on the number of pictures.
And for God's sake, use field names that make sense.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: PHP and MYSQL check empty values (any ideas appreciated)

Post by scarface222 »

Calm down pal, I am new at programming and inexperienced. I do have a table for images and one for people. They are associated by username but I thought each picture needs a field. How could you have unlimited pictures?
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: PHP and MYSQL check empty values (any ideas appreciated)

Post by ghogilee »

I think you might consider this option. One table for users, with another field name "directory". For example, when your user is registered, create some random unique number or word, and with that value create specific directory (How to create directory?) for him and store it in your users table.
Table structure example:
userid username pass directory
1 johndoe blah uniquestring

And then all pictures are stored in that specific folder on your server, and you don't have to worry about images table or 20, 30 fields for image paths.
I hope you understand this right :)
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: PHP and MYSQL check empty values (any ideas appreciated)

Post by scarface222 »

I think I understand but I still don't know how the user will gain access to each image. The way I have it set up, the user has a folder created that is their username with images, video, and othercontent folders within. How can the user find a specific image if only the directory is referenced. For example how would you display all the images in html if their names are not each stored in the database for reference. Is there some other way to do this by interacting with the folder instead? Thank you very much for your feedback though. I appreciate your time and help.
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: PHP and MYSQL check empty values (any ideas appreciated)

Post by ghogilee »

I would try to explain you through the code:

Code: Select all

 
<?php
/* Here is your registration script for example. I will do it really simple, but you will understand what is the purpose of storing the directory name in your table and how you can do it.
*/
$pass = md5($_POST['pass']);
// and then let's suppose that user is johndoe and that the field in registration form is called username
$username = $_POST['username']; //store the data
$username = strtolower(str_replace(" ", "", $username)); //for example, turn it to lowercase, replace empty spaces, trim, do it what you want
$random_number = rand(100, 999); //create random 3 digit number or whatever. read more about rand function on php.net 
$directory = $username . $random_number; //now we create a name for user directory merging username with random number to get it more unique
if (!is_dir('users/' . $directory)) { //if directory not exists
mkdir ("users/$directory", 0755, true); //create directory with the name we specified above. read more about mkdir function on php.net
}
//and now when we create our directory and enter the vales in database
//register user
$insert = mysql_query("INSERT INTO users (username, pass, directory) VALUES ('$username', '$pass', '$directory')");
if ($insert) {
  echo 'Our user is registered, directory created';
} else {
  echo 'Something goes wrong';
}
 
/* *********** USAGE ****************** */
// Now we have member.php page. when user is loged in, you will check it whatever you want, and on the member page you will READ the directory that we create earlier. Just take the user id from session or whatever//
 
$user_id = '25' //lets suppose that users id taken from session is 25
//Bellow we want to display pictures from that user but first take the name of our directory from database
$take_dir = mysql_query("SELECT directory FROM users WHERE userid = '$user_id' LIMIT 1");
while ($row = mysql_fetch_array($take_dir)) {
  $users_dir = $row['directory']; //store users directory name
}
//and just read the directory
//For example you have images in that directory and search just for .jpg extension
$desired_extension = 'jpg';
$dirname = 'users/' . $users_dir; //full directory if your users directory is found inside users dir from the root
$dir = opendir($dirname); //we OPEN that directory. Read more about OPENDIR function
 
// and now we take all the jpg images in loop and display them
 while(false != ($file = readdir($dir)))
  {
    if(($file != ".") and ($file != ".."))
    {
      $fileChunks = explode(".", $file); //separate name of the file to see the extension
      if($fileChunks[1] == $desired_extension) //interested in only jpeg extension (or whatever)
      { 
       //and finally display all the images
        echo '<img src="' . $dirname . $file . '" alt="' . $file . '"/>';
      } 
    }
  } 
  closedir($dir); //close the open directory
//and thats it
?>
 
So the main purpose of that script is to show how you can store just the name of directory in users table, and read the contents from it whenever you need it. Cheers ;)
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: PHP and MYSQL check empty values (any ideas appreciated)

Post by scarface222 »

Works great, I really appreciate your help bro, just curious though, why did you use a random number attached to each username folder. Why not just use the username alone? and how can you show more than one format at the same time? ex.gif, jpg.
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: PHP and MYSQL image management [solved]

Post by ghogilee »

For more formats, just create second $desired_extension1 = 'gif' and add in the corresponding "if" clause that extension and you're ready to roll :).
Random number is for purpose of making this directory more unique. maybe your login system demands login by email, and usernames may be duplicated, or for many different reasons. You may delete this ofcourse. This is just an example.
Enjoy. :drunk:
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: PHP and MYSQL image management [solved]

Post by scarface222 »

Great, thanks alot for all your help.
Post Reply