John McNeill
John McNeill

Reputation: 143

Inserting $_POST array into database

Columns in my database: stat_id, stat1, stat2, stat3.

I am using a form that allows me to loop through a list of members and input 3 different stats each. ($member_stat is an array already that includes 'stat_id' as one of its keys)

foreach($member_stats as $member_stat) {
    echo '<label for="stat1"> Stat 1</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat1" id="stat1" />';
    echo '<label for="stat2"> Stat 2</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat2" id="stat2" />';
    echo '<label for="stat3"> Stat 3</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat3" id="stat3" /><br />';
}

I hit submit and my $_POST array/data looks like this:

Array (

    [157stat1] = 1
    [157stat2] = 4
    [157stat3] = 7
    [158stat1] = 2
    [158stat2] = 2
    [158stat3] = 6
    [159stat1] = 8
    [159stat2] = 6
    etc...
)

My questions is: How do I take this $_POST array and insert it into my database as above? ie.

157 stat1 stat2 stat3
158 stat1 stat2 stat3
159 stat1 stat2 etc...

I have tried various things but am having a tough time wrapping my head around separating the stat_id from the different stats in the $_POST keys (ie: 157 from stat1, stat2, stat3). I am wondering if maybe I set it up wrong in the form and should be naming my inputs in some other way.

Upvotes: 0

Views: 316

Answers (3)

Shiloh Lawrence
Shiloh Lawrence

Reputation: 366

Use HTML arrays.

foreach($member_stats as $member_stat) {
    echo '<label for="stat1"> Stat 1</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat1]" id="stat1" />';
    echo '<label for="stat2"> Stat 2</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat2]" id="stat2" />';
    echo '<label for="stat3"> Stat 3</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat3]" id="stat3" /><br />';

}

This will return something like:

Array (

    [157stat][1] = 1
    [157stat][2] = 4
    [157stat][3] = 7
    [158stat][1] = 2
    [158stat][2] = 2
    [158stat][3] = 6
    [159stat][1] = 8
    [159stat][2] = 6
)

You could see an example at: http://www.thefutureoftheweb.com/blog/use-arrays-with-html-form-inputs and another great stack overflow answer here: HTML input arrays

Upvotes: 0

Cheery
Cheery

Reputation: 16214

$out = array();

$post= array(
 '157stat1' => 1,
 '157stat2' => 2,
 '157stat3' => 3,
 '158stat1' => 1
);

foreach($post as $key => $value)
{
    if (preg_match('/^(\d+)(\w+)$/', $key, $match) !== 0)
       $out[$match[1]][] = $match[2];
}

var_dump($out);

After that loop through $out and prepare SQL statements.

foreach($out as $key => $values)
{
  // escape array $values (array('stat1', 'stat2', 'stat3, ...) for each $key)
  // and string $key (it will be 157, 158 and so on)
  // prepare and execute SQL statement
  // function join will help to deal with array $values
}

Upvotes: 1

pedrofs
pedrofs

Reputation: 632

Imagine that your $_POST comes like that:

array(
    'stats' => array(
        157 => array(
            stat1 => 1,
            stat2 => 3,
            stat3 => 4
        ),
        158 => array(
            stat1 => 2,
            stat2 => 3,
            stat3 => 1
        )
        .
        .
    )
)

Then you could just:

foreach ($_POST['stats'] as $whateverId => $stats) {
    // now you have the desired $whateverId and
    // its $stats
    // you can access each stats indexing $stats like: $stats['stat1']
}

And finally you can accomplish the desired $_POST format using the following html form naming (using arrays notation):

foreach($member_stats as $member_stat) {
    $id = $member_stat['stat_id'];

    echo '<label for="stat1"> Stat 1</label>';
    echo "<input type=\"text\" name=\"stats[$id][stat1]\" id=\"stat1\" />";

    echo '<label for="stat2"> Stat 2</label>';
    echo "<input type=\"text\" name=\"stats[$id][stat2]\" id=\"stat2\" />";

    echo '<label for="stat3"> Stat 3</label>';
    echo "<input type=\"text\" name=\"stats[$id][stat3]\" id=\"stat3\" /><br />";
}

Don't forget to validate your $_POST['stats'] input.

Upvotes: 0

Related Questions