2014-10-08 138 views
0

我的数据库中的列:stat_id,stat1,stat2,stat3。

我正在使用一种表格,允许我遍历成员列表并输入3个不同的统计数据。 ($ member_stat已经是包括“stat_id”作为其关键之一数组)

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 />'; 
} 

我点击提交,我的$ _POST阵列/数据是这样的:

Array (

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

我的问题是:我如何把这个$ _POST数组并像上面那样插入到我的数据库中?即。

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

我曾尝试过各种东西,但我有一个艰难的时间环绕我的头周围从不同的统计在$ _ POST键分离stat_id(即:157从STAT1,STAT2,STAT3)。我想知道如果我在表单中设置了错误,并且应该以其他方式命名我的输入。任何有识之士将不胜感激。谢谢。

回答

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); 

之后通过$out循环并准备SQL语句。

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 
} 
+0

谢谢你的时间。我得到一个错误:$ out [$ match [1]] [] = $ match [2];你介意放弃这条线吗? – user2949794 2014-10-08 23:03:00

+0

@ user2949794你确定你阵列中的所有按键看起来都像“157stat1”吗?我不这么认为。好的,我会修改答案来考虑这一点。 – Cheery 2014-10-08 23:10:44

+0

是所有的键都是3位数(stat_id),后跟stat1,stat2或stat3。 – user2949794 2014-10-08 23:14:28

0

使用HTML数组。

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 />'; 

}

这将返回类似:

阵列(

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

你可以看到一个例子:http://www.thefutureoftheweb.com/blog/use-arrays-with-html-form-inputs和另一个伟大的堆栈溢出答案在这里:HTML input arrays

0

试想一下,你的$ _ POST来这样的:

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

,那么你可以只是:

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'] 
} 

最后,你可以使用下面的HTML表单完成所需的$ _ POST格式命名(使用数组符号):

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 />"; 
} 

不要忘记验证你的$ _POST ['stats']输入。