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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    33
    Thanks
    6
    Thanked 0 Times in 0 Posts

    PHP $_POST arrays with while loop

    Okay, here's what is going on...

    I have a php form that grabs information such as users from my mysql database and I am able to edit multiple users' information in one page. Lets say I have 5 users with three textboxes each and only one submit button at the bottom. How would I go about $_POST'ing and updating my mysql tables for the information I have inputed. I will have a hidden input value of the user's id. I understand this:

    PHP Code:
    <input name="username[]" value="<?=$r['user_name'?>" />
    I have the array setting in the input, just do not know where to go from there. I'm sort of a PHP noob but of course I am a fast learner and understand the lingo. "THX" for those who help!

    JoeMoe

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    1) Don't use short tags. This:

    Code:
    <input name="username[]" value="<?=$r['user_name'] ?>" />
    ought be:

    Code:
    <input name="username[]" value="<?php echo $r['user_name'] ?>" />
    You'd be better off setting each name with the userid or username as the key, i.e:

    Code:
    <input name="username[<?php echo $r['userid'].']" value="'.$r['user_name'] ?>" />
    And then you can loop through, using a foreach and assign correctly. If you use [], each key will just be an incremented number. Using the above, for example, you would use:

    Code:
    foreach ($_POST['username'] as $key => $val)
    {
        [$key is your userid and $val is the value]
    }
    Last edited by MattF; 03-18-2010 at 04:37 AM. Reason: Typo.

  • Users who have thanked MattF for this post:

    Joemoemofo (03-18-2010)

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    You could combine all data into one array using the same input name


    Untested, might be syntax errors
    PHP Code:
    $textbox = $_POST[username];
    $user_id = $_POST[user_id];

    foreach ($textbox as $value) {
    $username = $value[0]
    $textbox_one = $value[1]
    $textbox_two = $value[2]
    $textbox_three = $value[3]
    }

    mysql_query("INSERT INTO table (username, one, two, three)
    VALUES ('$username', '$textbox_one', '$textbox_two', '$textbox_three')
    WHERE user_id = $user_id ");


    <input name="username[]" value="<?=$r['user_name'?>" />
    <input name="username[]" value="" />
    <input name="username[]" value="" />
    <input name="username[]" value="" />


    ----------
    Last edited by Len Whistler; 03-18-2010 at 04:42 AM.
    Leonard Whistler

  • #4
    New Coder
    Join Date
    Jan 2009
    Posts
    33
    Thanks
    6
    Thanked 0 Times in 0 Posts
    @MattF, your code posted is perfect! Len, I didn't get a chance to try yours as Matt's is what I wanted. One thing, how would I go about a single checkbox with this array of material? Let's say if I wanted to make a user have the privilege to make comments on certain material I post on my website. The single checkbox is as follows:

    PHP Code:
    <input type="checkbox" class="boxed" value="1" name="user_comments[<?php echo $r['user_id'?>]" <? if($r['user_comments'] == "0") { echo ' '; } else { echo 'checked'; } ?> />
    The for each right now is:
    PHP Code:
    foreach ($_POST['user_comments'] as $key => $val)
    {
        if(empty(
    $val)) {
            
    $val2 "0"
            
    mysql_query("UPDATE users SET user_comments='$val2' WHERE user_id='$key'");
        } else { 
        
    mysql_query("UPDATE users SET user_comments='$val' WHERE user_id='$key'");
    } } 

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Joemoemofo View Post
    One thing, how would I go about a single checkbox with this array of material? Let's say if I wanted to make a user have the privilege to make comments on certain material I post on my website.
    One checkbox regardless of users? It shouldn't require an array if so, but the code below should suffice for the array method. Untested, btw.

    PHP Code:
    <input type="checkbox" class="boxed" value="1" name="user_comments[<?php echo $r['user_id'].']"'.(($r['user_comments'] == "0") ? ' checked' ''); ?> />

    PHP Code:
    if (isset($_POST['user_comments']) && !empty($_POST['user_comments']))
    {
        foreach (
    $_POST['user_comments'] as $key => $val)
        {
            if (!empty(
    $val) && intval($key))
            { 
                
    mysql_query('UPDATE users SET user_comments=\''.mysql_real_escape_string($val).'\' WHERE user_id='.intval($key));
            }
        }


  • #6
    New Coder
    Join Date
    Jan 2009
    Posts
    33
    Thanks
    6
    Thanked 0 Times in 0 Posts
    The code didn't work. It's alright, I just chose to drop the "FancyForm" style of my checkbox and went with a drop-down box. Thanks for the fix above, though.


  •  

    Posting Permissions

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