Jump to content

[SOLVED] Multiple form fields - I'm stumped.


Potatis

Recommended Posts

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.

 

points.jpg

 

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

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>

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");



?>

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.

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");



?>

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" } 

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");
?>

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! :)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.