inserting data into tables, linking and retrieving - help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

inserting data into tables, linking and retrieving - help!

Post 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.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post 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?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

bump**
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post 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?
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

Sorry, one other thing - how would I pass the foreign key through to the next page/table?
PanK
Forum Commoner
Posts: 36
Joined: Mon Nov 22, 2004 1:24 pm
Location: Richmond Hill, ON, Canada

Post 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>
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post 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.
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

bump***
Post Reply