0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
John McNeill
  • 143
  • 1
  • 12

3 Answers3

1
$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
}
Cheery
  • 16,063
  • 42
  • 57
  • Thank you for your time. I am getting an error: Undefined offset on the line with: $out[$match[1]][] = $match[2]; Would you mind dumbing down this line? – John McNeill Oct 08 '14 at 23:03
  • @user2949794 Are you sure that all keys of your array look like `157stat1` ? I do not think so.. Ok, I'll modify the answer to take this into account. – Cheery Oct 08 '14 at 23:10
  • yes all keys are 3 digits(stat_id) followed by either stat1, stat2 or stat3. – John McNeill Oct 08 '14 at 23:14
  • @user2949794 I do not think so, otherwise there will be no such error. Try the updated code. – Cheery Oct 08 '14 at 23:19
  • I put your code in new php file to test and didn't get an error. Must be something else I'm doing wrong. I'll play around with it. Thanks for your help! – John McNeill Oct 08 '14 at 23:28
  • @user2949794 use checkup with `if` anyway - the data from form can be forged and it will case this message. – Cheery Oct 08 '14 at 23:33
0

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

Community
  • 1
  • 1
Elly Post
  • 336
  • 3
  • 12
0

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.

pedrofs
  • 612
  • 1
  • 6
  • 14
  • This loop will create multiple `stat1` `id`s -- this would create an invalid HTML document. The behavior of the `for` attributes may be unexpected as well. – mickmackusa Sep 12 '22 at 03:26