forumnz Posted May 2, 2009 Share Posted May 2, 2009 I have a form with with 2 text fields. The user is able to add more fields using javascript. Basically, I am wondering how I would insert multiple rows into a database. Normally I would know how to do this, but the amount of rows is uncertain (could be 1 or 100). I suppose I would use an array? How would I do that? More info: the form would submit text fields named: name1 email1 name2 email2 name3 email3 etc... Thanks, Sam Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 <?php $name_count = $email_count = 1; while (isset($_POST['name' + $name_count])) { // do something $name_count++; } // same for email Get the hint? Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824557 Share on other sites More sharing options...
forumnz Posted May 2, 2009 Author Share Posted May 2, 2009 Oh one more thing! The user is able to delete sets of test fields (name and email), but each time a new one is created, a new number is generated. So basically the form might submit something like: name1 email1 name3 email3 name18 email18 Meaning all the numbers inbetween contain no data so they shouldn't be inserted - how would I get around this? Thanks! Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824559 Share on other sites More sharing options...
forumnz Posted May 2, 2009 Author Share Posted May 2, 2009 Thanks Ken2k7. Yes I understand that Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824561 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Oh great! I would like to say it's a JavaScript problem though! <?php foreach ($_POST as $key => $value) { if (strpos($key, 'name') === 0) { // do something } if (strpos($key, 'email') === 0) { // do something } } Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824565 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 Is there a way to cycle through the numbers and create an array with the existing numbers? Am I making this too complicated? Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824578 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 I'm just going to solve this topic and write a new (better explained) one Thanks Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824582 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Is there a way to cycle through the numbers and create an array with the existing numbers? Am I making this too complicated? Not complicated. Do you know PHP at all? It's not hard. We're not here to write code for you. Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824583 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 I do know a bit of PHP and I'm really good with a lot of things, I've just never really worked with arrays :/ I don't expect you to write code for me, I would be happy with the theory behind it Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824587 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Create 2 arrays (one for names and one for email) before the loop. Then inside the loop, inside each if statement, add them to the array. You know if the entry in the if statement means it's either name or email. Add them to the correct array. Link to comment https://forums.phpfreaks.com/topic/156601-solved-insert-into-database-question-multiple-rows/#findComment-824588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.