Potatis Posted January 6, 2009 Share Posted January 6, 2009 I have been trying to work out how to update points for multiple users at the same time. The picture below shows a table with usernames on the left (output from database using a while loop) and text fields on the right. I can do this with one person's name and points, by sending it to my processing page and using variables from $_POST to update the user's points in the database. When I have multiple usernames, and points text fields, I'm sure I need to put it into some sort of array, but I don't know how. I have tried to search, but have only found code that expects me to know exactly how many names would be in the array, but the names are dynamically generated from the database. The names can be added or removed at any time so there is not exact number. I have also looked at implode and explode, but can't find any code that seems to suit what I am trying to do. Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/ Share on other sites More sharing options...
dezkit Posted January 6, 2009 Share Posted January 6, 2009 Post relevant code Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730723 Share on other sites More sharing options...
Potatis Posted January 6, 2009 Author Share Posted January 6, 2009 Well, I don't know the code. Below is the table I made for the graphic above and the form I tried. It's working code for one name, but not multiple names. <form id="form1" name="form1" method="post" action="alter_points_process.php"> <table width="300" border="1" class="mrtable" align="center"> <tr> <?php //Fetch usernames from the database $result = mysql_query("SELECT * FROM points WHERE rank='student' ORDER BY username ASC") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $username = $row['username']; echo "<td align='center'>"; echo $username; echo "<input name='username' type='hidden' value='$username' />"; echo "</td>"; echo "<td width='20'>"; echo "<input name='points' type='text' value='0' size='5' maxlength='5' />"; echo "</td>"; echo "</tr>"; } ?> </table> <p> </p> <div align="center"> <input name="Submit" type="submit" value="Submit Points" /> </div> </form> Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730727 Share on other sites More sharing options...
Potatis Posted January 6, 2009 Author Share Posted January 6, 2009 And this is my processing page: <?php require_once("includes/constants.php"); ?> <?php require_once("includes/connection.php"); ?> <?php $username = $_POST['username']; $points = mysql_real_escape_string($_POST['points']); mysql_query("UPDATE `points` SET `points` = (points + {$points}) WHERE `username` = '$username' ;"); header("location: alter_points.php"); ?> Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730732 Share on other sites More sharing options...
gevans Posted January 6, 2009 Share Posted January 6, 2009 You need to make the form field names an array so; <input type="text" name="name[]" /> Then you will get an array in your POST data which you can loop through to update all your users Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730758 Share on other sites More sharing options...
Potatis Posted January 6, 2009 Author Share Posted January 6, 2009 Thanks gevans. I tried adding [] to the name before. It was still giving me one name and score, because I don't know how to get the names and points out of the arrays. That's when I was investigating explode, but I must have been on the wrong track, because I couldn't find code that would would get the data from the array and put it into the database. I'm still searching though. Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730762 Share on other sites More sharing options...
gevans Posted January 6, 2009 Share Posted January 6, 2009 can you try this and post what you get after submitting the form? <?php require_once("includes/constants.php"); ?> <?php require_once("includes/connection.php"); ?> <?php echo "<pre>".var_dump($_POST['points'])."</pre>"; exit(); $username = $_POST['username']; $points = mysql_real_escape_string($_POST['points']); mysql_query("UPDATE `points` SET `points` = (points + {$points}) WHERE `username` = '$username' ;"); header("location: alter_points.php"); ?> Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730771 Share on other sites More sharing options...
Potatis Posted January 6, 2009 Author Share Posted January 6, 2009 Yes indeed! I typed in order of the names in the picture above 5, 10, 15, 20 and got: array(4) { [0]=> string(1) "5" [1]=> string(2) "10" [2]=> string(2) "15" [3]=> string(2) "20" } And with username too: array(4) { [0]=> string(4) "Bert" [1]=> string( "Big Bird" [2]=> string(5) "Ernie" [3]=> string(5) "Oscar" } array(4) { [0]=> string(1) "5" [1]=> string(2) "10" [2]=> string(2) "15" [3]=> string(2) "20" } Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730774 Share on other sites More sharing options...
gevans Posted January 6, 2009 Share Posted January 6, 2009 So all your number are coming through to the code. Have you also amde the username field in the form an array --name='username[]' If so the following code should work; <?php require_once("includes/constants.php"); require_once("includes/connection.php"); for($i=0;$i<=count($_POST['username']);$i++) { $username = $_POST['username'][$i]; $points = mysql_real_escape_string($_POST['points'][$i]); mysql_query("UPDATE `points` SET `points` = (points + {$points}) WHERE `username` = '$username' ;"); } header("location: alter_points.php"); ?> Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730778 Share on other sites More sharing options...
Potatis Posted January 6, 2009 Author Share Posted January 6, 2009 So all your number are coming through to the code. Have you also amde the username field in the form an array Yes, I already added the [] And YES your code works! Thank you so much! Now I will study the code to make sure I understand it! I really appreciate your help! Link to comment https://forums.phpfreaks.com/topic/139660-solved-multiple-form-fields-im-stumped/#findComment-730782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.