Maintaining referential integrity using php mysql

Discussion in 'PHP' started by viron86, Aug 23, 2010.

  1. #1
    i have two from 1]vendor and 2]vendor address detail
    a vendor can have one or more address detail so what i have done is i have made two different tables for vendor and vendor address with vendor_id(autoincrement) as primary key and foreign key.

    what i want is when data is inserted in vendor table i want that auto generated primary key to be inserted as foreign key in address table

    i am using one logic where after inserting into vendor table i sort it desecnding order and extract last inserted record and use that primary key but this would fails if two users are performing this operation simultanously.

    can anyone suggest me some better logic
     
    viron86, Aug 23, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    You can use mysql_insert_id to retrieve that certain ID:

    
    <?php
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('mydb');
    
    mysql_query("INSERT INTO mytable (product) values ('kossu')");
    printf("Last inserted record has id %d\n", mysql_insert_id());
    ?>
    
    
    PHP:
     
    ThePHPMaster, Aug 23, 2010 IP
  3. viron86

    viron86 Active Member

    Messages:
    426
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #3
    thanks
    i had no idea about this function
     
    viron86, Aug 23, 2010 IP