Input fields from a database

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
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Input fields from a database

Post by xionhack »

Hello. I need help with something I am doing. Lets say that I have to tables "member" and "color", the "member" has "member_id, first_name, last_name" the "color" table has "color_id, color".

What I want is to call all the members that dont have a color assigned, and next each name a select box with all the colors, and when they select one and press a button the "color" field in the "member" table will be updated with the color selected for that particular member. Please let me know if you understand! thanks!
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Input fields from a database

Post by Stryks »

First off, how are you going to link the user to the color? Assuming it's just a single color for each user (1 to 1) then you should add color_id to the users table (or even user_id to the colors table - user table makes more sense though).

Then you can pull your un-colored users with something like ...

Code: Select all

SELECT * FROM members WHERE color_id = NULL;


I think that should work anyhow.

Then loop through all of the results, creating the select lists as you go. You should be able to then pick up any alterations on submit and apply them as needed.

I'm not really sure as to how much detail to go into, as I'm not sure where you are in terms of PHP / HTML knowledge.

Hope this helps anyhow.
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Input fields from a database

Post by xionhack »

Hello. Right now I have the list of the members and the input box next to them with the colors, I'm fine till there. My problem is at the moment of updating the database, I am stuck on how to update those that have a color selected, and leave alone the ones that dont have any color selected.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Input fields from a database

Post by Stryks »

Can you post a little code from the page that creates the lists and the script that is submitted to. It'll make it much easier to point you in the right direction.

But assuming that your selects are created in the format of ...

Code: Select all

<select name="usercolor[<? echo $user_ID; ?>]">
  <option value="" selected="selected"></option>
  <option value="1">Goldenrod</option>
  <option value="2">Fuchia</option>
  <option value="3">Peuce</option>
</select>
Then you should be able to pull the values for your users from the form with ...

Code: Select all

if(isset($_POST['usercolor'])) {
    foreach($_POST['usercolor'] as $user_id=>$color_id) {
        if(ctype_digit($user_id) && ctype_digit($color_id)) {
            if(!($color_id == "")) {
                $sql = 'UPDATE members SET color_id = ' . mysql_real_escape_string($color_id) . ' WHERE user_id = ' . mysql_real_escape_string($user_id);
                // do the query
            } // else skip this item because no color was set
        } // else abort because values have been tampered with
    }
}
Something like that anyhow ...

You'll want to validate the user doing the changing though. If this is an open access form, anyone can change anyone else's color. I'm assuming this is for admin use, in which case they should be allowed full access, where everyone else only has access to their own user_id.

Hope this helps.
Post Reply