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 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    36
    Thanks
    5
    Thanked 0 Times in 0 Posts

    checking for empty fields

    Please bare with me while I try to explain this.

    For the site I am creating, I am allowing my users the ability to upload a max of 20 photos. When they create their account a row is automatically inserted into the photo table for them. Like this

    Code:
    mysql_query ("
    INSERT INTO photos VALUES ('$_SESSION[id]','', '', '', '', '','','','','','','','','','','','','','','','') ");

    This inserts their user id and then the rest of the fields in the row are left blank (so they can upload their photos at a later time). I assume by the way I inserted , that the 20 fields are empty strings (please correct me if I'm wrong).

    So, when the user goes to upload a photo I want to basically check for the first empty field, if it is empty insert the photo, if not check for the next empty field and so forth.

    I have tried various things to get this to work the way I want, but to no avail.. this is the most recent code I have.

    Code:
    if($submit){
    $query = "SELECT * FROM photos WHERE id='$_SESSION[id]' ";
    $result = mysql_query($query);
    while ($field = mysql_fetch_assoc($result)) {
    if($field[p1]=''){
    mysql_query ("INSERT INTO photos (p1) VALUES('$pic')");
    }    
    }
    }
    I just can't seem to wrap my head around how to achieve this, any help would be greatly appreciated.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I have to disagree with your design. You need to normalize the data, and in this case that means create a separate row for every photo a user uploads. Tie it to the user using the user_id. This way you do not need to store empty fields.

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    36
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    I have to disagree with your design. You need to normalize the data, and in this case that means create a separate row for every photo a user uploads. Tie it to the user using the user_id. This way you do not need to store empty fields.

    Thanks. before I posted this thread, that thought actually crossed my mind. But since I already had it in place it was worth it to me to see if there was a solution for it.

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    normalize the data, your queries will be much easier as the database grows. You'll spend less time normalizing your data than you will using a workaround.


  •  

    Posting Permissions

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