SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    insert data into a child table using mysql_insert_id

    Hello,

    Can someone please help me with using the mysql_insert_id function in mysql..I have two tables in which one is child to other and I am trying to insert data from the same form but the data has to be separated into those two tables.

    I could only get the data in one table but in the child table i could not update the value of the foreign key. instead i get an error FUNCTION mydb.mysql_insert_id does not exist

    Any help will be appreciated..thank you

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    mysql_insert_id is not a mysql function, but a php function: PHP: mysql_insert_id - Manual

  3. #3
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for correction just noted that...and again how do i use that function

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Did you take a look at the manual and the examples there?
    You do the insert in the main table, then get the inserted id, then use that value for the insert in the second table (the one with the foreign key).

  5. #5
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I have and here is what I have been trying to use it.....


    //prepare query to insert data into table drivers
    $sql = "INSERT INTO drivers(driverRegNo,schoolId, surname, othernames, DOB, address, gender, placeOfBirth, telephoneNo) VALUES ('$driverRegNo','$schoolId', '$surname', '$othernames', '$DOB', '$address', '$gender', '$placeOfBirth', '$telephoneNo')";
    $driver_Id = mysql_insert_id();
    //process query
    $result = mysql_query($sql) or die(mysql_error());

    //query to insert data into second table
    $sql2= "INSERT INTO licence (issueDate, driverID,licenceClass, issuingAuthority, expiryDate) VALUES ('$_POST[issueDate]', '$driver_Id', '$_POST[licenceClass]','$_POST[issuingAuthority]','$_POST[expiryDate]')";

    //process query
    $result1 = mysql_query($sql2) or die(mysql_error());

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Put this line
    Code:
    $driver_Id = mysql_insert_id();
    AFTER you've executed the first query.

  7. #7
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much......it works now


Bookmarks

Posting Permissions

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