tutorialstuff Posted March 2, 2008 Share Posted March 2, 2008 I have a formthat I want to seperate the information and store the data in two differnt tables but I'm not quite sure how to do it. below I pasted a var_dump on the $_POST of the form in action. (You can see the form here: http://techguymike.com/window/test/new-line.php ) I want to create 1 record in a table called quotes with the information of (first_name, last_name, address, city state zip, phone 1, phone 2, phone 3 and email then I want to create multiple records in a table called quote_items with (qty, window, width, height and room) and if you notice they each incriment such as qty1 qty2 qty3 etc... any ideas on how I can accomplish this? array(30) { ["first_name"]=> string(3) "Bob" ["last_name"]=> string(4) "Dole" ["address"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["zip"]=> string(0) "" ["phone_1"]=> string(3) "555" ["phone_2"]=> string(3) "555" ["phone_3"]=> string(4) "5555" ["email"]=> string(13) "bob@yahoo.com" ["qty1"]=> string(1) "3" ["window1"]=> string(6) "Awning" ["width1"]=> string(2) "12" ["height1"]=> string(2) "14" ["room1"]=> string(10) "Location 1" ["qty2"]=> string(1) "5" ["window2"]=> string(6) "Awning" ["width2"]=> string(1) "5" ["height2"]=> string(2) "55" ["room2"]=> string(10) "Location 3" ["qty3"]=> string(2) "45" ["window3"]=> string(11) "French Door" ["width3"]=> string(1) "4" ["height3"]=> string(1) "3" ["room3"]=> string(10) "Location 4" ["qty4"]=> string(1) "3" ["window4"]=> string(25) "3 lite Sliding Patio Door" ["width4"]=> string(2) "33" ["height4"]=> string(1) "3" ["room4"]=> string(10) "Location 1" } Quote Link to comment https://forums.phpfreaks.com/topic/93962-form-help/ Share on other sites More sharing options...
phpSensei Posted March 2, 2008 Share Posted March 2, 2008 I dont understand what your trying to do, just put the info in different vars and insert them in different tables. Or am i missing something here... Is the qty1 qty2 qty3 your problem? Quote Link to comment https://forums.phpfreaks.com/topic/93962-form-help/#findComment-481429 Share on other sites More sharing options...
Psycho Posted March 2, 2008 Share Posted March 2, 2008 This should give you a start. Although i would add a lot more verification of the data: <?php if ($_POST) { $fname = mysql_real_escape_string($_POST['first_name']); $lname = mysql_real_escape_string($_POST['last_name']); $addr = mysql_real_escape_string($_POST['address']); $city = mysql_real_escape_string($_POST['city']); $state = mysql_real_escape_string($_POST['zip']); $zip = mysql_real_escape_string($_POST['state']); $ph1 = mysql_real_escape_string($_POST['phone_1']); $ph2 = mysql_real_escape_string($_POST['phone_2']); $ph3 = mysql_real_escape_string($_POST['phone_3']); $email = mysql_real_escape_string($_POST['email']); $query = "INSERT INTO quotes (first_name, last_name, address, city, state, zip, phone_1, phone_2, phone_3 and email) VALUES ('$fname', '$lname', '$addr', '$city', '$state', '$zip', '$ph1', '$ph2', '$ph3', '$email')"; $result = mysql_query($query); $i = 1; while ($_POST['window'.$i]) { $qty = mysql_real_escape_string($_POST['qty'.$i]); $window = mysql_real_escape_string($_POST['window'.$i]); $width = mysql_real_escape_string($_POST['width'.$i]); $height = mysql_real_escape_string($_POST['height'.$i]); $room = mysql_real_escape_string($_POST['room'.$i]); $window = mysql_real_escape_string($_POST['window'.$i]); $i++; $query = "INSERT INTO quote_items (qty, window, width, height, room) VALUES ('$qty', '$window', '$width', '$height', '$room')"; $result = mysql_query($query); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/93962-form-help/#findComment-481439 Share on other sites More sharing options...
tutorialstuff Posted March 2, 2008 Author Share Posted March 2, 2008 I want to create multiple records (entries into the database) so it will probably be some sort of foreach loop. example would be: mysql_query("INSERT INTO quote (quote_id, fname, lname, physical_address, physical_city, physical_state, physical_zip, mailing_address, mailing_city, mailing_state, mailing_zip, phone, email) VALUES('', '$first_name', '$last_name', '$address', '$city', '$state', '$zip', '$phone', '$email')"); then mysql_query("IINSERT INTO quote_items(item_id, quote_id, qty, window, width, height, room) VALUES('', 'the quote_id from the quote table', '$_POST['qty1']', '$_POST['window1'], '$_POST['width1'], '$_POST['height1'], '$_POST['room1'])"); mysql_query("IINSERT INTO quote_items(item_id, quote_id, qty, window, width, height, room) VALUES('', 'the quote_id from the quote table', '$_POST['qty2']', '$_POST['window2'], '$_POST['width2'], '$_POST['height2'], '$_POST['room2'])"); and so on. It's a 1 to many relationship (quote table = one) (quote_item table = many) I hope this explnation makes more sense Quote Link to comment https://forums.phpfreaks.com/topic/93962-form-help/#findComment-481448 Share on other sites More sharing options...
tutorialstuff Posted March 2, 2008 Author Share Posted March 2, 2008 This should give you a start. Although i would add a lot more verification of the data: <?php if ($_POST) { $fname = mysql_real_escape_string($_POST['first_name']); $lname = mysql_real_escape_string($_POST['last_name']); $addr = mysql_real_escape_string($_POST['address']); $city = mysql_real_escape_string($_POST['city']); $state = mysql_real_escape_string($_POST['zip']); $zip = mysql_real_escape_string($_POST['state']); $ph1 = mysql_real_escape_string($_POST['phone_1']); $ph2 = mysql_real_escape_string($_POST['phone_2']); $ph3 = mysql_real_escape_string($_POST['phone_3']); $email = mysql_real_escape_string($_POST['email']); $query = "INSERT INTO quotes (first_name, last_name, address, city, state, zip, phone_1, phone_2, phone_3 and email) VALUES ('$fname', '$lname', '$addr', '$city', '$state', '$zip', '$ph1', '$ph2', '$ph3', '$email')"; $result = mysql_query($query); $i = 1; while ($_POST['window'.$i]) { $qty = mysql_real_escape_string($_POST['qty'.$i]); $window = mysql_real_escape_string($_POST['window'.$i]); $width = mysql_real_escape_string($_POST['width'.$i]); $height = mysql_real_escape_string($_POST['height'.$i]); $room = mysql_real_escape_string($_POST['room'.$i]); $window = mysql_real_escape_string($_POST['window'.$i]); $i++; $query = "INSERT INTO quote_items (qty, window, width, height, room) VALUES ('$qty', '$window', '$width', '$height', '$room')"; $result = mysql_query($query); } } ?> The problem with this one though is that they aren't always in order it could be qty3, qty7, qty9 rather thean just qty1, qty2, qty3. So I can't just use qty$i. I need to figure out away to put them in an array some how or something. Quote Link to comment https://forums.phpfreaks.com/topic/93962-form-help/#findComment-481451 Share on other sites More sharing options...
Psycho Posted March 2, 2008 Share Posted March 2, 2008 The problem with this one though is that they aren't always in order it could be qty3, qty7, qty9 rather thean just qty1, qty2, qty3. So I can't just use qty$i. I need to figure out away to put them in an array some how or something. Then don't make them qty1, qty2, etc. And instead make them an array to begin with...sheesh. (Although if you used proper validation on your page they WOULD be qty1, qty2, etc.) Just name the fields "qty[]", "window[]", "width[]", etc. and the values will be returned as sub arrays. Quote Link to comment https://forums.phpfreaks.com/topic/93962-form-help/#findComment-481463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.