Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-18-2010, 02:55 AM   PM User | #1
Joemoemofo
New Coder

 
Join Date: Jan 2009
Posts: 33
Thanks: 6
Thanked 0 Times in 0 Posts
Joemoemofo is an unknown quantity at this point
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
Joemoemofo is offline   Reply With Quote
Old 03-18-2010, 03:22 AM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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 03:37 AM.. Reason: Typo.
MattF is offline   Reply With Quote
Users who have thanked MattF for this post:
Joemoemofo (03-18-2010)
Old 03-18-2010, 03:32 AM   PM User | #3
Len Whistler
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
Len Whistler is on a distinguished road
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="" />


----------
__________________
Leonard Whistler

Last edited by Len Whistler; 03-18-2010 at 03:42 AM..
Len Whistler is offline   Reply With Quote
Old 03-18-2010, 04:06 AM   PM User | #4
Joemoemofo
New Coder

 
Join Date: Jan 2009
Posts: 33
Thanks: 6
Thanked 0 Times in 0 Posts
Joemoemofo is an unknown quantity at this point
@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'");
} } 
Joemoemofo is offline   Reply With Quote
Old 03-18-2010, 09:32 AM   PM User | #5
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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));
        }
    }

MattF is offline   Reply With Quote
Old 03-18-2010, 07:33 PM   PM User | #6
Joemoemofo
New Coder

 
Join Date: Jan 2009
Posts: 33
Thanks: 6
Thanked 0 Times in 0 Posts
Joemoemofo is an unknown quantity at this point
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.
Joemoemofo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:34 AM.


Advertisement
Log in to turn off these ads.