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

Recommended Answers

All 4 Replies

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');

yeah..the form_id in table form is auto_increment..


ok..will try..
thaxs so much

seems not work bro

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.

$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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.