为什么我的数组只返回最后一行

时间:2012-08-17 17:34:28

标签: php mysql arrays

我搜索了网站,但无法找到答案或与我如何设置查询有关。这是我处理发布数据并传递给db的文件。我的表格包裹着桌子。表中的单元格由数组填充。当我离开这个页面时,我希望表格单元格中的值写入db中的新表格。表中的每一行代表db中的一个单独的行。

这是我的所有帖子数据来自我的表单。

    <form action="post_movement.php?class_id=<?php echo $class_id; ?>&pagenum=<?php echo $pagenum; ?>" method="post">
     <table border=1 width=800px>
 <tr>
 <th width=300px>Client</th>
 <th width=50px>Order</th>
 <th width=200px>Movements</th>
 <th>Sets/Reps/Seconds</th>  
 <th width=150px>Rest</th>
 <th>X Completed</th>
 </tr>

<?php

// Get workout class and output in table format -------------------------------------------------

if(empty($workout_class) === false)
{   
foreach($workout_class as $wc){
if ($wc['pagenum'] !== $pagenum) continue 1;

    echo '<tr>
        <td><input type="hidden" name="first_name[]" value="'.($wc['first_name']).'">'. ($wc['first_name']).'
        <span><input type="hidden" name="nickname[]" value="'.($wc['nickname']).'">('. ($wc['nickname']).')</span>
        <span><input type="hidden" name="last_name[]" value="'.($wc['last_name']).'">'. ($wc['last_name']).'</span>
        </td>
              <td><input type="hidden" name="order[]" value="'.($wc['order']).'">'. ($wc['order']). '</td>
              <td>
              <select name="movement[]" width=200>
              <option>'. ($wc['mv_00']). '</option>
              <option>'. ($wc['mv_01']). '</option>
              <option>'. ($wc['mv_02']). '</option>
              <option>'. ($wc['mv_03']). '</option>
              <option>'. ($wc['mv_04']). '</option>
        </select></td>
        <td><input type="hidden" name="rep_set_sec[]" value="'.($wc['rep_set_sec']).'">'. ($wc['rep_set_sec']). '</td>
        <td><input type="hidden" name="rest[]" value="'.($wc['rest']).'">'. ($wc['rest']). '</td>
        <td>00</td>
        </tr>';
  } // foreach($data_array
    //print_r($data_array);
} // if(!empty

?>

<! End Table ---------------------------------------------------------------------------------->
</table>
<input type="submit" value="Complete Movement">

</form>

这是我的php处理表单中的帖子数据并将其提交给查询。

    if (empty($_POST)=== false){
$movement_data = array('user_id', 'class_id', 'class_name', 'first_name', 'last_name', 'nickname', 'order', 'movement', 'rep_set_sec', 'rest', 'date');
foreach($movement_data as $key => $value){
     $movement_data = array(
        'user_id'   => $session_user_id,
        'class_id'  => $_GET['class_id'],
        'class_name'    => $class_name,
        'first_name'    => $_POST['first_name'],
        'last_name'     => $_POST['last_name'],
        'nickname'  => $_POST['nickname'],
        'order'     => $_POST['order'],
        'movement'  => $_POST['movement'],
        'rep_set_sec'   => $_POST['rep_set_sec'],
        'rest'      => $_POST['rest'],
        'date'      => $today
        );
    }

    completed_movement($movement_data);
    //header('Location: new_client.php');
    //exit ();

    }

这是我的查询,它将POST数据插入db。

    function completed_movement($movement_data){
array_walk($movement_data, 'array_sanitize');

$fields = '`' . implode('`, `', array_keys($movement_data)) . '`';
$data = '\'' . implode('\', \'', $movement_data) . '\'';


$query = mysql_query ("INSERT INTO `completed_movements` ($fields) VALUES ($data)");

}

HTML表单页面有两行数据表,看起来像这样。

    Colby (Big Cheese) Dodson | 1A | Key Movement | Sets/Reps/Seconds | Rest|
    -------------------------------------------------------------------------
    Mike (Big Mac) Mckenzie   | 1A | Key Movement | Sets/Reps/Seconds | Rest|

var_dump($ _POST)产生这个。我可以看到那里的价值只是不确定它是否应该是它应该的。

    array(7) { 
           ["first_name"]=> array(2) { 
               [0]=> string(5) "Colby" 
               [1]=> string(4) "Mike" } 
           ["nickname"]=> array(2) { 
               [0]=> string(10) "Big Cheese" 
               [1]=> string(7) "Big Mac" } 
           ["last_name"]=> array(2) { 
               [0]=> string(6) "Dodson" 
               [1]=> string(8) "McKenzie" } 
           ["order"]=> array(2) { 
               [0]=> string(2) "1A" 
               [1]=> string(2) "1A" } 
           ["movement"]=> array(2) { 
               [0]=> string(12) "Key Movement" 
               [1]=> string(12) "Key Movement" } 
           ["rep_set_sec"]=> array(2) { 
               [0]=> string(17) "Sets/Reps/Seconds" 
               [1]=> string(17) "Sets/Reps/Seconds" } 
           ["rest"]=> array(2) { 
               [0]=> string(4) "Rest" 
               [1]=> string(4) "Rest" } 
           }

当它执行var_dump时它应该是什么样的?

我需要我的数组的每一行看起来像这样。前三个值是user_id,class_id和&amp; class_name并不是我在上一页的html表单的一部分。这些值在解析脚本页面上被拾取并与我的结果的每一行一起传递。

    $row = array(29, 48, Class 1, Colby, Big Cheese, Dodson, 1A, Key Movement, Sets/Reps/Secons, Rest)

我的表单中的每个条目都需要这样的行。

1 个答案:

答案 0 :(得分:4)

while($row = mysql_fetch_assoc($query));
return $movement_data;

print_r($movement_data);

您的while循环遍历整个数组 - 没有评估代码。完成后,它返回最后一个值,因为这是变量中唯一的项。您的print_r永远不会被评估。

编辑添加:

foreach($movement_data as $key => $value);

你的foreach声明中还有一个尾随分号。这将产生相同的结果。

再次编辑:

每个表单值都作为值数组传递;所以你需要做的是逐步完成数组,然后逐个处理它们。下面的代码应该可以工作,但我没有测试过。

它在$_POST["first_name"]数组上循环 - 对于每个值,它填充一个临时数组,并将其传递给您的函数。其中一些值是硬连线的,有些是来自等效数组索引 - 第一次运行使用值为0的索引元素,依此类推。

$i = 0;
while (isset($_POST["first_name"][$i])) {
    $movement_data = array(   
        'user_id'   => $session_user_id,   
        'class_id'  => $_GET['class_id'],   
        'class_name'    => $class_name,   
        'first_name'    => $_POST['first_name'][$i],   
        'last_name'     => $_POST['last_name'][$i],   
        'nickname'  => $_POST['nickname'][$i],   
        'order'     => $_POST['order'][$i],   
        'movement'  => $_POST['movement'][$i],   
        'rep_set_sec'   => $_POST['rep_set_sec'][$i],   
        'rest'      => $_POST['rest'][$i],   
        'date'      => $today   
        );   

    completed_movement($movement_data);

    $i++;
}
相关问题