insert into one main table and one detail table in one go without double records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Miley
    New Member
    • Oct 2009
    • 1

    insert into one main table and one detail table in one go without double records

    Hi everyone,
    This is my question/problem.

    I have a table with orders and a table with order details.
    Clients can order multiple products at once during one order, so one line has to be inserted in the main order table, and multiple order details from the same order in the order detail table.

    How do I do this?

    Right now if a client orders 5 products during one order, the order detail table looks fine, but also get 5 records in my main order table. It has to be one.
    I probably have to change my SQL statement for this, but don't really know how.

    Any suggestions?

    (renatef_bestel lingen is the main ordertable, renatef_besteld etail is the order detail table)

    A part of my current code:

    Code:
    $sql = "INSERT INTO renatef_bestellingen (KLANT_ID, besteldatum, besteluur, leveringswijze, leveringskost) VALUES ('$klantid', '$datum', '$uur','$leveringswijze', '$leveringskost')";
    $theresult = mysql_query($sql);
    
    $bestelid = mysql_insert_id(); 
    		
    $sql2 = "INSERT INTO renatef_besteldetail (bestelling_id,product_id,aantal)VALUES (".$bestelid.",'$key','$val')";
    $theresult2 = mysql_query($sql2);
    about the attachments

    the 'renatef_bestel lingen' picture shows that 2 records are made with the same info, it should only be 'BESTELLING_ID' with value 26

    in the 'renatef_bestel detail' picture everything is correct except for the 'bestelling_id' which both should be value 26

    I also added a print screen of more of my code.
    thanks in advance for your help! I keep looking myself but it is really urgent so any help is very welcome.
    Attached Files
    Last edited by Dormilich; Oct 11 '09, 06:46 PM. Reason: Please use [code] tags when posting code
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Welcome to Bytes. Your image resolution was really bad so I can't see your code. I am a little confused as to what you want... Can you not do something like:
    Code:
    /* Insert row into renatef_bestellingen */
    
    foreach ( $items as $item ) {[indent]/* Insert row into renatef_besteldetail for each detail */[/indent]
    }

    Comment

    Working...