Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question [PHP][MySQL] Inserting in the first tbl doesn't insert in the second tbl.

    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??

    PHP Code:
        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...

    PHP Code:
    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()); 
    Last edited by TAViX; 03-05-2009 at 09:02 AM. Reason: update

  • #2
    New Coder
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    1) Try adding a semicolon before the select statement
    2) Try adding the finishing bracket after batch_info.nickname

    eg:

    PHP Code:
    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()); 

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #4
    New Coder
    Join Date
    Feb 2009
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by Fumigator View Post
    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:

    PHP Code:
    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...

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •