love_bug Posted September 16, 2010 Share Posted September 16, 2010 Hi again , I am stuck with simplest problem of array .. i searched all forums and couldn't find similar problem.. Iam collecting array post data "data m_name[]" from a form, but I don't know how to get 2 array variables at the same time, here's what I tried : <form action="" method="post" name="myForm"> <div> <input type="text" name="m_name[]" /> <input type="text" name="m_name[][email]" /> </div> <div> <input type="text" name="m_name[]" /> <input type="text" name="m_name[][email]" /> </div> <div> <input type="text" name="m_name[]" /> <input type="text" name="m_name[][email]" /> </div> <input type="submit" name="button" id="button" value="Submit" /> </form> other page where I want to receive above array <?php if($_POST) { print_r($_POST['m_name']); // Output is : Array ( [0] => someone [1] => Array ( [email] => 3 ) [2] => someone two [3] => Array ( [email] => 4 ) [4] => someone three [5] => Array ( [email] => 5 ) ) while (list ($key,$val) = @each ( $_POST['m_name'])) { echo $val.val2 ???"<br/>"; // here I want to get both value $_POST['m_name'] and $_POST['m_name'] ['email'] since I am putting them in db << } } ?> I want to get both values of $_POST['m_name'], I don't know how to do it or is there another way to do it? thankx in advance!! phpfreaks have been very helpful .. Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/ Share on other sites More sharing options...
Pikachu2000 Posted September 16, 2010 Share Posted September 16, 2010 If it was me, I believe I'd do something like the code below, for which a print_r($_POST) will output this: [pre] Array ( [name] => Array ( [0] => (first name value) [1] => (second name value) [2] => (third name value) ) => Array ( [0] => (first email value) [1] => (second email value) [2] => (third email value) ) [button] => Submit ) [/pre] In other words, the first set of fields would be $_POST['name'][0] and $_POST['email'][0] the next would be $_POST['name'][1] and $_POST['email'][1], etc., etc. <form action="" method="post" name="myForm"> <div> <input type="text" name="name[]" /> <input type="text" name="email[]" /> </div> <div> <input type="text" name="name[]" /> <input type="text" name="email[]" /> </div> <div> <input type="text" name="name[]" /> <input type="text" name="email[]" /> </div> <input type="submit" name="button" id="button" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/#findComment-1111678 Share on other sites More sharing options...
love_bug Posted September 16, 2010 Author Share Posted September 16, 2010 hi thanks for reply.. yea i thought about that too, but i want to put those values together in db.. while (each $val1, $val2) { $sql = "INSERT INTO table name (name,email)($val1,$val2); } It will be great if i could combine 2 array like below so i can put them in sql db with while loop.. just tell me it cant be done in php lol .. thankx Array ( [0] => $val1, $val2 [1] => $val1, $val2 [2] => $val1, $val2 ) Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/#findComment-1111698 Share on other sites More sharing options...
Pikachu2000 Posted September 16, 2010 Share Posted September 16, 2010 Can you clarify a bit just how you want these to be inserted into the DB table? Are you planning to have each of the pairs inserted as an individual record? Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/#findComment-1111726 Share on other sites More sharing options...
love_bug Posted September 16, 2010 Author Share Posted September 16, 2010 Hi, yea similar to : http://stackoverflow.com/questions/842956/php-foreach-loop-through-multidimensional-array but the solution he provided doesn't apply to my problem i guess.., only thing I want to do is get array of "m_name[] and m_name[]" and insert them in db with loop. here's array output : Array ( [0] => someone [1] => Array ( [email] => someone@gmail.com ) [2] => sometwo [3] => Array ( [email] => sometwo@gmail.com ) [4] => somethree [5] => Array ( [email] => somethree@gmail.com ) ) Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/#findComment-1111737 Share on other sites More sharing options...
roopurt18 Posted September 16, 2010 Share Posted September 16, 2010 <form action="" method="post" name="myForm"> <div> <input type="text" name="person[0][name]" /> <input type="text" name="person[0][email]" /> </div> <div> <input type="text" name="person[1][name]" /> <input type="text" name="person[1][email]" /> </div> <div> <input type="text" name="person[2][name]" /> <input type="text" name="person[2][email]" /> </div> <input type="submit" name="button" id="button" value="Submit" /> </form> <?php // $_POST will then essentially look like this array: $arr = array( array( 'name' => 'Bob', 'email' => 'bob@domain.com' ), array( 'name' => 'Sally', 'email' => 'sally@domain.com' ), array( 'name' => 'Fred', 'email' => 'fred@domain.com' ) ); // Make your sql statement $insert_sql = "insert into `mytable` ( `name`, `email` ) values "; // Add values parts $parts = array(); foreach( $arr as $person ) { $parts[] = sprintf( "( '%s', '%s' )", mysql_real_escape_string( $person[ 'name' ] ), mysql_real_escape_string( $person[ 'email' ] ) ); } // Append the parts $insert_sql .= implode( ', ', $parts ); // Run query $rv = mysql_query( $insert_sql ); // ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/#findComment-1111749 Share on other sites More sharing options...
sasa Posted September 16, 2010 Share Posted September 16, 2010 try <?php foreach ($_POST['m_name'] as $key => $value) if($key % 2 == 0) $tmp = "('$value', "; else $out[] = $tmp."'$value[email]')"; $sql = "INSERT INTO table name (name,email) ". implode(', ', $out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/#findComment-1111848 Share on other sites More sharing options...
love_bug Posted September 17, 2010 Author Share Posted September 17, 2010 Ahh.. thank everyone for the help, especially sasa, his solution works like charm.. thankx Quote Link to comment https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/#findComment-1111967 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.