Jump to content

Recommended Posts

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 ..

Link to comment
https://forums.phpfreaks.com/topic/213574-array-values-to-insert-in-db/
Share on other sites

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>

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
)

 

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
        )

)

 

 

<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 );
// ...
?>

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.