•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 426,882 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,291 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser: Programming Forums
Views: 390 | Replies: 4
![]() |
•
•
Join Date: Feb 2008
Posts: 22
Reputation:
Rep Power: 1
Solved Threads: 0
hi all..i had problem here..hope someone can help me..
i`ve create a db that contains several tables. myproblem now the foreign key doesnt parallel with primary key. i had create table named form which form_id is the PK..then table named register which id is the PM.i put form_id in table register as FK..however the number of PK n FK not parallel which when i key in data,the PK generate number but FK yet 0.same goes when i key in more data..FK still 0.what the problem actually??FYI,
table form
form_id as PK
cat_id
event_title
event_venue
form_details
form_date
table register
id as PK,
form_id as FK
name
email
contact
mate
comment
what i plan to do after this is goin to GET data from table register according to the form_id.
ca somebody help me??
have greatday
i`ve create a db that contains several tables. myproblem now the foreign key doesnt parallel with primary key. i had create table named form which form_id is the PK..then table named register which id is the PM.i put form_id in table register as FK..however the number of PK n FK not parallel which when i key in data,the PK generate number but FK yet 0.same goes when i key in more data..FK still 0.what the problem actually??FYI,
table form
form_id as PK
cat_id
event_title
event_venue
form_details
form_date
table register
id as PK,
form_id as FK
name
contact
mate
comment
what i plan to do after this is goin to GET data from table register according to the form_id.
ca somebody help me??
have greatday
I assume form_id in the form table is AUTO_INCREMENT'ed.
After inserting into form, you need to retrieve the form_id from the newly inserted row and use this in the insert statement to insert into the register table.
It won't happen automatically.
Luckily mysql has the "LAST_INSERT_ID()" function to make this easier - e.g.
After inserting into form, you need to retrieve the form_id from the newly inserted row and use this in the insert statement to insert into the register table.
It won't happen automatically.
Luckily mysql has the "LAST_INSERT_ID()" function to make this easier - e.g.
insert into form values(..whatever...); insert into register (form_id, name, email, contact, mate, comment) values ( LAST_INSERT_ID(), 'joe bloggs', 'joe@hotmail', '555-555-555','','hello');
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation:
Rep Power: 8
Solved Threads: 240
What is the scenario like ? You insert a record to form table and then insert one/many records to register table ?
If thats the case, then, blater's code should work. You can also try this.
If thats the case, then, blater's code should work. You can also try this.
php Syntax (Toggle Plain Text)
$insert_to_form = "insert into form (col1, col2) values ('1','2')"; mysql_query($insert_to_form); //since form has an autoincrement field, the last record inserted will have the maximum value (of the auto incremented field) $get_last_autoincrement_id = "select max(id) as last_id from form"; $res = mysql_query($get_last_autoincrement_id); $row = mysql_fetch_array($res); $last_id = $row['last_id']; //then insert into register table $insert_to_register = "insert into register (form_id, col1,col2 ) values ('$last_id','1','2')"; mysql_query($insert_to_register); $insert_another_record_to_register = "insert into register (form_id, col1,col2 ) values ('$last_id','3','4')"; mysql_query ($insert_another_record_to_register);
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
•
•
•
•
•
•
•
•
DaniWeb MySQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: select syntax for variable tablename??
- Next Thread: MYSQL with VB6 and crystal reports



Linear Mode