Page 1 of 1

inserting data into tables, linking and retrieving - help!

Posted: Thu Nov 25, 2004 10:11 am
by TheOracle
Sorry - I appear to be becoming a nuisance on this forum, buy hey I am a total newb!

I have quite a complex form (over 100 fields) which I have split over 4 pages.

I think it mmakes more sense to have each of those pages insert into a different table, but my question is, once on the second page, how can I link the data from the first table to the data on this table, in order to retrieve the correct data when select a row from all 4 tables?

Any ideas most appreciated.

Posted: Thu Nov 25, 2004 10:30 am
by MarK (CZ)
My idea: Put the data already retreived in an array and then set them as hidden fields. Bit of coding:

Code: Select all

<?php

$form_values = Array(         // array with form values
  "var1" => $_POST["var1"],
  "var2" => $_POST["var2"],
  "var3" => $_POST["var3"]
);


$output = "";  // buffer the output
forEach ($form_values as $var_name => $var_value) {
  $output .= "<input type='hidden' name='$var_name' value='$var_value' />\r\n"; // add hidden field with data
  // single quotes in the data should be also handled here, this is not complete code
}
echo $output; // print the buffer
?>
This should print all the data you've put into that array as hidden fields, so they'll be send to the next page too! :)
Don't forget to validate the data on the last page before using them in db (because they can be changed by editting HTML between pages).
And also use method='post' for such a big amount of data cause GET has some size limitations.

Alternatively you could probably use sessions for storing the data

Hope this helps

Posted: Thu Nov 25, 2004 10:44 am
by phpScott
in your first table create a unique Id when you first insert the date (auto-increment works well for this) then use that as a foriegn key in the rest of the tables.

table 1
uniqueId
name
address

table 2
table1UniqueId
email
phone

table 3
table1UniqueId
lastMovieWatched
lastBookRead

table 4
table1UniqueId
comment
otherStuff.

then all you have to pass is the uniqueId from page to page or store it in a session or what ever you want to do with it.

Posted: Thu Nov 25, 2004 10:45 am
by TheOracle
Thanks Mark

Only problem with that is (should have said earlier). Because of the size of the form and the number pages, I decided to provide the user with a save and exit button, as well as save and continue, so they can retrieve and complete their application form at a later time.

This means I need to dump the data into a table before proceeding.

I just can't get my head around how to join the table from the first page with the table from the second, third and fourth pages.

Any other suggestions?

Posted: Thu Nov 25, 2004 10:48 am
by phpScott
bump**

Posted: Thu Nov 25, 2004 10:57 am
by TheOracle
Thanks phpScott, I must have missed you entry first time round (you beat me this time!).

Only question I have is do you know how to do this through phpMyAdmin? Can you specify a foreign key and the table you are pointing to when creating a table?

Posted: Thu Nov 25, 2004 11:03 am
by TheOracle
Sorry, one other thing - how would I pass the foreign key through to the next page/table?

Posted: Thu Nov 25, 2004 12:41 pm
by PanK
I think, that you should write in tp the table in each page, and sent around only ID of the record.
<a href="somefile.php?foregnkey=<?=$foreignkey?>"> NEXT </a>

Posted: Thu Nov 25, 2004 1:57 pm
by TheOracle
How would define this variable $foreignkey though, in the code??

Would it be easier to do it with session variables, and if so, could someone show me how? I already have session_start() in my include file, so do I just add a $_SESSION['loan_id'] = (where do I get this value??)

Hope someone can ellaborate a bit further.

Posted: Thu Nov 25, 2004 3:44 pm
by TheOracle
bump***