Jump to content

get assigned number from sql insert for another insert


piznac

Recommended Posts

Prehaps this is a backwards way of doing this. So if Im being dense please don't hesitate to say so. So here is what I have:

 

function insert_multi($item_num,$item_desc,$item_qty,$emb_loc1,$num,$size,$artwork){
$flds = array( 'item_num' , 'item_desc' , 'item_qty', 'num', 'size');
for($i=0;$i<count($_POST['item_qty']);$i++) {
    $qtmp = array();
    foreach($flds as $fld)
        if (trim(stripslashes($_POST[$fld][$i])) != '')
            $qtmp[] = $fld . " = '" . mysql_escape_string(trim(stripslashes($_POST[$fld][$i]))) . "'";
    $q = "insert into c_or_details set " . implode(', ',$qtmp);
    $rs = mysql_query($q) or die ('Problem with 1 the query: ' . $q . '<br>' . mysql_error());	
}
}

 

As you can see Im inserting an indermined amount of records into a mysql table. Next I need to insert the remaining data "$artwork & $emb_loc1" into another table. Which dosent really pose a problem. But with each entry of the last sql statement an auto increment value defines each entry. I need that auto number to insert with the other data into the new table. Does that make sense.

 

Basically I have a main table and a sub table. The main table holds the main data on an order. The sub table holds "options" of the order. But to be able to pull them back I need a primary key to match the key in the main table. Anyone know an easy way to get this data from the first table to the other?

Link to comment
Share on other sites

I suppose I could do a query before the insert to see what is the last auto number. Then count the number of inserts and add one to each (incrementally) and then use those numbers. This seems highly inaccurate though,.. is there a better way?

Link to comment
Share on other sites

<?php
foreach ($insert_array as $insert) {
        mysql_query($insert);
        $ids[] = mysql_insert_id();
}

print_r($ids);
?>

 

Simple as that given that each insert is in an array or looped through some how.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.