cmaclennan Posted June 4, 2009 Share Posted June 4, 2009 I have a multi part form that submits portions of the data to 4 different tables however when submitted everything is entered into the databases fine expcept in one part where it adds it in twice. Any suggestions would be appreciated. // Make the query. $query = "INSERT INTO customers (customer_id, company, contact, address, address2, city, state_prov, zip_postal, country, phone, email, fax, tax_id) VALUES ('', '$co', '$cn', '$ad' , '$ad2', '$ct', '$sp', '$zp', '$cou', '$ph', '$em', '$fx', '$tx')"; $result = @mysql_query ($query); // Run the query. $customer_id = mysql_insert_id(); $query = "INSERT INTO shipping (shipping_id, sh_company, sh_contact, sh_address, sh_address2, sh_city, sh_state_prov, sh_zip_postal, sh_country, sh_phone, sh_email, sh_fax, customer_id) VALUES ('', '$cos', '$cns', '$ads' , '$a2s', '$cts', '$sps', '$zps', '$cous', '$phs', '$ems', '$fxs', '$customer_id')"; $result = @mysql_query ($query); // Run the query. $shipping_id = mysql_insert_id(); $query = "INSERT INTO orders (order_id, date, po, invoice_date, invoice, sent_mfg, req_date, final_date, shipped_via, tracking, shipping_id) VALUES ('', '$dt', '$po', '$idt' , '$in', '$mfg', '$re', '$fn', '$sh', '$tr', '$shipping_id')"; $result = @mysql_query ($query); // Run the query. $order_id = mysql_insert_id(); $result = @mysql_query ($query); // Run the query. foreach($_POST as $key => $value) { //looping through all posted fields if(strpos($key, 'partid_') !== false) { //found a part field, need to find out the _x number, then insert the entire row $suffix = substr($key, -1); //this gets the _x number $partid = $_POST['partid_' . $suffix]; $quantity = $_POST['quantity_' . $suffix]; $desc = $_POST['desc_' . $suffix]; $serial = $_POST['serial_' . $suffix]; $query = "INSERT INTO parts_ordered (partid, quantity, `desc`, serial, order_id) VALUES ('$partid', '$quantity', '$desc', '$serial', '$order_id')"; $result = mysql_query($query); } } if ($result) { // If it ran OK. Link to comment https://forums.phpfreaks.com/topic/160972-solved-form-submits-to-database-twice/ Share on other sites More sharing options...
zer0day Posted June 4, 2009 Share Posted June 4, 2009 I'm guessing that the table that's getting it twice is "orders"? Link to comment https://forums.phpfreaks.com/topic/160972-solved-form-submits-to-database-twice/#findComment-849527 Share on other sites More sharing options...
cmaclennan Posted June 4, 2009 Author Share Posted June 4, 2009 you would guess correct, what did i do?? ??? Link to comment https://forums.phpfreaks.com/topic/160972-solved-form-submits-to-database-twice/#findComment-849534 Share on other sites More sharing options...
zer0day Posted June 4, 2009 Share Posted June 4, 2009 You're running that query twice. $query = "INSERT INTO orders (order_id, date, po, invoice_date, invoice, sent_mfg, req_date, final_date, shipped_via, tracking, shipping_id) VALUES ('', '$dt', '$po', '$idt' , '$in', '$mfg', '$re', '$fn', '$sh', '$tr', '$shipping_id')"; $result = @mysql_query ($query); // Run the query the first time. $order_id = mysql_insert_id(); $result = @mysql_query ($query); // Run the query the second time. foreach($_POST as $key => $value) Link to comment https://forums.phpfreaks.com/topic/160972-solved-form-submits-to-database-twice/#findComment-849536 Share on other sites More sharing options...
cmaclennan Posted June 4, 2009 Author Share Posted June 4, 2009 crap...does it matter which I remove? Link to comment https://forums.phpfreaks.com/topic/160972-solved-form-submits-to-database-twice/#findComment-849544 Share on other sites More sharing options...
zer0day Posted June 4, 2009 Share Posted June 4, 2009 Nope. Link to comment https://forums.phpfreaks.com/topic/160972-solved-form-submits-to-database-twice/#findComment-849546 Share on other sites More sharing options...
cmaclennan Posted June 4, 2009 Author Share Posted June 4, 2009 Thanks a lot always nice to have a fresh pair of eyes. Link to comment https://forums.phpfreaks.com/topic/160972-solved-form-submits-to-database-twice/#findComment-849552 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.