Code:
Table A
A_id A_fname B_id C_id
1 Joe 1 2
2 Kelly 1 2
3 Mary 2 1
4 Peter 3 3
5 Paul 2 1
Code:
Table B
B_id B_add B_pcode
1 georgetown 12345
2 Ellaville 22334
3 Montague 55467
Code:
Table C
C_id C_state
1 CA
2 FA
3 DC
Ok.. what i got here are 3 tables where table B and C are using a foreign keys to relate themselves to table A. This is the type of results i am looking for.
I wrote a php form to request user to enter in their particulars and table B and C are the common fields so whenever user enter the similar address and postcode, the related id from table B will reflect on table A. However, what i had here was whenever user enter the address and postcode, a new id is generated even though the same address and postcode had entered a few times. Below is my code.
[PHP]$B_insert = "INSERT INTO B (B_add, B_pcode)
VALUES ('$val_add', '$val_pcode')";
$result = mysql_query($B_insert) or die (mysql_error());
$B_insert = mysql_insert_id() or die (mysql_error());
$C_insert = "INSERT INTO C (C_state)
VALUES ('$val_state')";
$result = mysql_query($C_insert) or die (mysql_error());
$C_insert = mysql_insert_id() or die (mysql_error());
$A_insert = "INSERT INTO A (A_fname, A_orange, A_melon)
VALUES ('$val_fname', '$B_insert', '$C_insert')";
$result = mysql_query($apple_insert) or die (mysql_error()); [/PHP]