User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Feb 2008
Posts: 22
Reputation: jinchiruki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jinchiruki jinchiruki is offline Offline
Newbie Poster

key problem

  #1  
Apr 14th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2008
Posts: 25
Reputation: blater is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
blater's Avatar
blater blater is offline Offline
Light Poster

Re: key problem

  #2  
Apr 15th, 2008
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.

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');
Reply With Quote  
Join Date: Feb 2008
Posts: 22
Reputation: jinchiruki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jinchiruki jinchiruki is offline Offline
Newbie Poster

Re: key problem

  #3  
Apr 15th, 2008
yeah..the form_id in table form is auto_increment..


ok..will try..
thaxs so much
Reply With Quote  
Join Date: Feb 2008
Posts: 22
Reputation: jinchiruki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jinchiruki jinchiruki is offline Offline
Newbie Poster

Re: key problem

  #4  
Apr 15th, 2008
seems not work bro
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: key problem

  #5  
Apr 16th, 2008
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.
  1. $insert_to_form = "insert into form (col1, col2) values ('1','2')";
  2. mysql_query($insert_to_form);
  3. //since form has an autoincrement field, the last record inserted will have the maximum value (of the auto incremented field)
  4. $get_last_autoincrement_id = "select max(id) as last_id from form";
  5. $res = mysql_query($get_last_autoincrement_id);
  6. $row = mysql_fetch_array($res);
  7. $last_id = $row['last_id'];
  8. //then insert into register table
  9. $insert_to_register = "insert into register (form_id, col1,col2 ) values ('$last_id','1','2')";
  10. mysql_query($insert_to_register);
  11. $insert_another_record_to_register = "insert into register (form_id, col1,col2 ) values ('$last_id','3','4')";
  12. 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*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MySQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 10:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC