>Hallo everyone
>I am, as one might say, 'near yet so far'....
>* I have a page of user subscription info generated on the fly
>* I want an administrator to check a box on each result set
>* The info from each checked result set should be inserted into
>another table (A new row/tuple for
>each user)
>I am currently using each form element as an array to hold x
>number of Names, Adresses etc. Where
>'x' is the number of results generated, and consequently the
>number of checkboxes generated.
>The processing script loops thru this and inserts only those
>results that have been checked by the
>administrator in inserted in a new row.
>Using $HTTP_POST_VARS and dumping that to the page displays
>(obviousely) 'array', 'array', etc.
>As each hidden form element is an array and also an
>$HTTP_POST_VARS element)
>I need some way to extract the value of each array's keys so that
>they can be used in an SQL INSERT.
>I'm using MySQL-3.22.32 and php4.0.3pl1
>Here is what I have as the processing script, but as you should be
>able to see it is immedietley
>very limiting:
>while (list($key,$val) = each($Name)) {
>    $insertsql = "INSERT INTO $table_user (Name) VALUES ('$val')";
>    $result = mysql_query($insertsql, $connection) or die(mysql_error());
>    }
>This simply INSERTS the values in the Name array, into a new row
>for each name held in the array.
>Is it possible to somehow extend this loop or use a different kind
>of loop to insert all the array
>values in this manner?
>Ideally it would work like this (This is JUST to illustrate the
>point, I KNOW it won't work!!)
>while (list($key,$val) = each($Name,$Address,$Age,$Course)) {
>    $insertsql = "INSERT INTO $table_user (Name,Address,Age,Course) VALUES
>    ('$valName','$valAddress','$valAge','$valCourse')";
>    $result = mysql_query($insertsql, $connection) or die(mysql_error());
>    }
>Can anyone make any suggestions that would help me here?
>Many thanks in advance!

I'm not sure how you are naming the variables on your form but I suggest
using something like:

<INPUT TYPE="CHECKBOX" NAME="form[0][save]"    VALUE="YES">
<INPUT TYPE="TEXT"     NAME="form[0][name]"    VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[0][address]" VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[0][age]"   VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[0][course]"   VALUE="blahblahblah">

<INPUT TYPE="CHECKBOX" NAME="form[1][save]"    VALUE="YES">
<INPUT TYPE="TEXT"     NAME="form[1][name]"    VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[1][address]" VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[1][age]"   VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[0][course]"   VALUE="blahblahblah">

<INPUT TYPE="CHECKBOX" NAME="form[2][save]"    VALUE="YES">
<INPUT TYPE="TEXT"     NAME="form[2][name]"    VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[2][address]" VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[2][age]"   VALUE="blahblahblah">
<INPUT TYPE="TEXT"     NAME="form[0][course]"   VALUE="blahblahblah">

Now when you receive this form you'll get all the stuff you want in one
variable, in this case $form.

To insert into the database:

while (list($key,$val) = each($form)) {
  if ($key[save] == "YES") {
    $insertsql = "INSERT INTO $table_user (Name,Address,Age,Course)
                  VALUES ('$key[name]', '$key[address]', 'key[age]',
    $result = mysql_query($insertsql, $connection) or die(mysql_error());

NB code is untested, use with caution :)

Jason Wong
Gremlins Associates
Tel: +852-2573-5033
Fax: +852-2573-5851

PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to