seannie — 2013-01-16T07:25:23-05:00 — #1
I have a table called members and a number of foreign keys in this table for the purpose of joining from other tables.
Examples of the foreign ids are ageid,genderid. In the age and gender tables there are accompanying values for the ids (ie. agevalue = 24, gendervalue=female)
When I'm doing a sql insert query can I allow for someone to complete a form by adding the values rather than ids and then populate the member table with the respective id.
Would I have to use a subquery within the insert select using a where clause to specify inserting id where value is returned.
guido2004 — 2013-01-16T08:12:03-05:00 — #2
In the case of gender, I would put a select box in the form with the two allowed values (male and female) displayed to the user to choose from, and the corresponding id in the value property of the options.
In the case of age, I really don't see the use of foreign keys and an "age" table?
gk53 — 2013-01-16T15:19:55-05:00 — #3
I think to keep age in database is not really good idea. Age is variable DOB never changes and should be in database if you need to know age...
seannie — 2013-01-18T06:56:06-05:00 — #4
Thanks for your help.
I'm still getting an issue when I'm trying to insert foreign keys when respective value is entered. Here's what I have do. I'd appreciate any assistance:
-Values posted from form which I put into variables:
-Extract foreign key values usiing seperate sql statements:
$type_result="select type_id from tblType where type_desc='$type_desc'";
$dept_result="select dept_id from tblDept where dept_desc='$dept_desc'";
$sect_result="select sect_id from tblSect where sect_desc='$sect_desc'";
-Insert foreign key values where the respecftive value was entered:
$result = "INSERT INTO tblTest (type_id,dept_id,sect_id)
guido2004 — 2013-01-18T07:08:32-05:00 — #5
How do the users enter the values in the form? Do they type them in? Or do they choose from a list?
seannie — 2013-01-18T09:14:48-05:00 — #6
They type the values into an input box.
guido2004 — 2013-01-18T15:41:37-05:00 — #7
Then how can you be sure the value exists in the respective tables?
If you want to give the user the possibility to add any value, then there's no need for foreign keys and other tables.
If you want them to choose one of the existing values from the type, dept or sect table, then just load those values in a select box, and put the foreign key in the value property of the option, like this:
<option value="key1">value 1</option>
<option value="key2">value 2</option>
<option value="key3">value 3</option>
This way, in your script you'll already have the foreign key value.