Re: php - posting multiple items

by "Michael Gerholdt" <gerholdt(at)fredonia.edu>

 Date:  Sun, 21 Oct 2001 09:51:14 -0400
 To:  <Keith(at)webgraffix.com>,
<hwg-languages(at)hwg.org>
 References:  webgraffix
  todo: View Thread, Original
Keith,

I don't think this is a php issue. Instead I think it is in HTML and SQL
that the problem lies. But php might help you solve some of it.

When you say you have repeating tables, what you really have is something
else. In an html table, you have table rows being repeated.

In a form, you have form elements being repeated. These elements are created
via php script but I don't think that is relevant to the problem you are
experiencing.

You repeat form elements, and they have the same name. First  you say (for
example):
DaYear = 2002
DaMonth = January
DaDay = Monday
menu = rice and beans

Then  you use the same form element names and say:

DaYear = 2002
DaMonth = January
DaDay = Tuesday
menu = goulash

Then you use the same form element names and say:

DaYear = 2002
DaMonth = January
DaDay = Wednesday
menu = hamburgers

And you add the hidden inputs

<input type="hidden" name="Returned" value="1">
<input type="hidden" name="SchoolLocationID" value="MiddleSchool1">
<input type="hidden" name="LunchMenuID" value="">
<input type="submit" value="Add Menu">

Then you submit this.

I believe that what happens in this case is that this will get passed:

DaYear = 2002,2002,2002
DaMonth =January,January,January
DaDay = Monday,Tuesday,Wednesday
menu = rice and beans,goulash,hamburger
Returned = 1
SchoolLocationID = MiddleSchool
LunchMenuID =

When you say that only the last go-round of form elements is being
submitted, then that makes it seem like the variable values are not being
appended into arrays as I just said, but rather overwriting the former
values, so that therefore only the last inputs to the variable survive:

DaYear = 2002
DaMonth =January
DaDay = Monday
menu = hamburger
Returned = 1
SchoolLocationID = MiddleSchool
LunchMenuID =

Really, though, I think they get passed as an array. You can do some testing
on that - just echo the variables to the page and see. (Temporarily change
the <form ...> tag to have action="newPage.php" and on that newPage.php just
display any form elements and values being returned to it.)

Assuming I am correct - and I just tested again to confirm that I am - it
comes through as an array - then somehow your php code must be just grabbing
the last item off the array. Still, the php code itself is  not the problem.

First identify the problem, then work on a solution -

So what might solutions be?

Clearly you need to have form elements with different names for each
"meal-group" but those names have to tie the meal-groups to themselves:

DaYear1
DaMonth1
DaDay1
menu1

DaYear2
DaMonth2

etc might be one way.

Then on the return pass you need to do some conditional stuff. You probably
already are doing conditional stuff that you didn't display in your code
snippet as this indicates:

    // the checks are complete, now we can insert
    mysql("$DBName","INSERT INTO LunchMenu VALUES(
    '$Menu','$Date','$SchoolLocationID','LunchMenuID')") or
die(mysql_error());

I'll just pretend that all  the checking is wrapped up in If Not
IsEmpty(DaYear) but you'll want to do it all for each possible "menu-group"
I expect.


Here's pseudocode

IF NOT (IsEmpty(DaYear1)) {
// do your previous checking here for the "1" items
// stuff into slightly different variable names i.e.
// '$Menu1','$Date1','LunchMenuID1'
 mysql("$DBName","INSERT INTO LunchMenu VALUES(
 '$Menu1','$Date1','$SchoolLocationID','LunchMenuID1')") or
die(mysql_error());
}

IF NOT IsEmpty(DaYear2) {
// do your previous checking here for the "2" items
// stuff into slightly different variable names i.e.
// '$Menu2','$Date2','LunchMenuID2'
 mysql("$DBName","INSERT INTO LunchMenu VALUES(
 '$Menu2','$Date2','$SchoolLocationID','LunchMenuID2')") or
die(mysql_error());
}

and so you can iterate through checks and submit any number of menu-groups
if they have content, and ignore them if not.

There may be a more elegant way to do this - probably is - but this should
be some interesting work as a learning exercise.

It might be enticing to leave the form just the way it is and let the items
come over as an array, and then pluck them out and group them according to
where they are in the arrays. But the problem with this approach is that the
form elements don't come across in any specific order, so you won't know
which order the arrays are in - they won't have any connection to each
other.

Again, your assertion that only the last set if data gets inserted makes me
suspicious that my assumptions here aren't quite right. Are you certain it
is always just the very last, and not perhaps a mixture of all three? If the
latter were the case, I'd be more certain that I'm on the right track.

Luck -

Regards,
Michael Gerholdt
SUNY College at Fredonia

>
> Presently when you attempt to add a "Lunch Menu" item, you can only add
> 1 item at a time.  I have created a page that contains 2 separate tables
> to allow a user to add menus for 2 dates at once (my ultimate goal is to
> allow for adding up to 15 items at once).  I simply copied the table
> from the existing working page that contains only 1 table.
>
>  The code snippet for what I've done thus far can be viewed at
> http://www.schoolpad.net/webadmin/23addLunch2.phps .  The problem I'm
> running into is that only the information from the last table gets
> posted into the database table.
>

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA