...

View Full Version : [PHP][MySQL] Inserting in the first tbl doesn't insert in the second tbl.



TAViX
03-05-2009, 08:02 AM
Ok, I have 2 tables let's say first one is called batch_info with the following entries: batch_id (Primary Key - PK), trainee_num (FK), nickname (FK), etc, and passport_id with those entries: passport_id (PK), trainee_num (PK), nickname (PK), etc...

As you can see in the first table, batch_id, trainee_num and nickname, are linked to the values from the second table.

Now, I have this script that introduce the values in passport_info table, but the problem is that this script doesn't also update/insert the values from trainee_num and nickname also in the batch_info table, even if the entries are linked...

MySQL:

batch_info Links to: trainee_num: dbrev00.passport_info.trainee_num ON DELETE: cascade ON UPDATE: cascade
trainee_num: dbrev00.passport_info.nickname ON DELETE: cascade ON UPDATE: cascade

So my question is, how to update this code in order to have the same values in both tables??



mysql_query("INSERT INTO passport_info
(trainee_num, sur_name, given_names, middle_name, nickname)
VALUES ('".clean($tc)."', '".clean($sn)."', '".clean($gn)."','".clean($mn)."','".clean($nn)."')" )
or die(mysql_error());




Thanks in advance.

EDIT:
I have update the code a little but with no effects... :confused:



mysql_query("INSERT INTO passport_info
(trainee_num, sur_name, given_names, middle_name, nickname)
VALUES ('".clean($tc)."', '".clean($sn)."', '".clean($gn)."','".clean($mn)."','".clean($nn)."') SELECT batch_info.trainee_num, batch_info.nickname FROM batch_info WHERE (passport_info.trainee_num = batch_info.trainee_num) AND (passport_info.nickname = batch_info.nickname" )
or die(mysql_error());

jimjamjo1
03-05-2009, 04:46 PM
1) Try adding a semicolon before the select statement
2) Try adding the finishing bracket after batch_info.nickname

eg:


mysql_query("INSERT INTO passport_info
(trainee_num, sur_name, given_names, middle_name, nickname)
VALUES ('".clean($tc)."', '".clean($sn)."', '".clean($gn)."','".clean($mn)."','".clean($nn)."'); SELECT batch_info.trainee_num, batch_info.nickname FROM batch_info WHERE (passport_info.trainee_num = batch_info.trainee_num) AND (passport_info.nickname = batch_info.nickname)" )
or die(mysql_error());

Fumigator
03-05-2009, 04:51 PM
Referential integrity does not do row inserts, it just prevents rows from being inserted (or updated) that don't follow the rules. You still have to run two insert queries.

TAViX
03-06-2009, 01:58 AM
Referential integrity does not do row inserts, it just prevents rows from being inserted (or updated) that don't follow the rules. You still have to run two insert queries.

Try with two queries but no results. Only uploads in the first table:



mysql_query("INSERT INTO passport_info (passport_id, trainee_num, sur_name, given_names, middle_name, nickname) VALUES ('', '".clean($tc)."', '".clean($sn)."', '".clean($gn)."','".clean($mn)."','".clean($nn)."'") or die(mysql_error());
mysql_query("INSERT INTO batch_info (batch_id, trainee_num, nickname) VALUES ('', '".clean($tc)."', '".clean($nn)."'") or die(mysql_error());



Pls help, what else do I need to add here? I was thinking maybe to use JOIN, but doesn't seems to work either...

Fumigator
03-06-2009, 05:02 PM
I'm surprised you're not seeing an error of any kind. Or are you?

Try inserting the rows manually outside of PHP using phpMyAdmin or something like that and see what happens.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum