Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting multiple fields from a POST form 1

Status
Not open for further replies.

andnos

Technical User
Nov 21, 2005
48
US
Hello,

I have a dilemma that I've been working on for the last two days that I can't seem to solve, and this forum has be really helpful in the past:

Here is what I'm trying to do:

1 - pull multiple fields from a database, these fields are all similar - got this done

2 - display it as a html form for updating - got this done

3 - submit the changed/updated information back to the database. this is where I'm having problems. Let's say I'm updating the field named "name" in the database, so for example I have a form with 5 fields and they all are named "name". I can also pull the "name_id" from the database, which is unique, if that is needed.

Now the problem that I'm facing is after the form is submitted, how do I go through all of the "name" fields and update them in the database to reflect the changes.

If you need more information please let me know, I can also provide the code if that is requested.

Thanks in advance,
Andy
 
There is a little-known, obscurely-documented feature of PHP that might be of help here.

If you have multiple fields of the same name, then name them as if they were PHP arrays:

<form method="post"....>
<input type="text" name="person_name[1]"><br>
<input type="text" name="person_name[2]"><br>
<input type="text" name="person_name[3]"><br>
<input type="text" name="person_name[4]"><br>
<input type="text" name="person_name[5]"><br>
.
.
.
</form>

Then $_POST['person_name'] will itself be an array. If your database has a field which records a unique ID for each record, you can use those as the subscripts in the field names in your form. That way, all you have to do is use a while() loop across the values in $_POST['person_name'] and have the IDs to use in your UPDATE queries.

And additional thing I would do is to store the original data in a hidden field:

<form method="post"...>
<input type="text" name="person_name[1]">
<input type="text" name="origonal_person_name[1]"><br>
<input type="text" name="person_name[2]">
<input type="text" name="origonal_person_name[2]"><br>
<input type="text" name="person_name[3]">
<input type="text" name="origonal_person_name[3]"><br>
<input type="text" name="person_name[4]">
<input type="text" name="origonal_person_name[4]"><br>
<input type="text" name="person_name[5]">
<input type="text" name="origonal_person_name[5]"><br>
.
.
.
</form>

That way, your script can compare the field with what was originally stored and change only those records in the database which the user changes. You could also store the original field data in session variables.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks,

I took your advice and then used this code to break it up and insert it back into the database:

Code:
foreach($arrayname as $key=>$value)
    {
    echo $key.": ".$value;
    ***SQL CODE HERE
    }

I'm posting it here in case somebody else might need it.

Andy
 
sleipnir214 thanks for that info. I was just trying to figure out a clever way to do something using check boxes and this is gonna help a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top