列表框将变量值从MySQL传递给数组

时间:2012-07-02 08:54:06

标签: php mysql html

<form action = "settings.php" method  = "post">
Student ID*:
<input type = "text" name = "std_id" value = "<?php echo $user_data['std_id'];?>"><br>
Firstname*:
<input type = "text" name = "name" value = "<?php echo $user_data['name'];?>"><br>
Surname*:
<input type = "text" name = "surname" value = "<?php echo $user_data['surname'];?>"><br>
Group*:
<select name = "group" id = "group">
    <option value="0"><-- Please Select Item --></option>
    <?php 
    $show=mysql_query("SELECT * FROM room_group_options ORDER BY op_id");
    while ($array = mysql_fetch_array($show))
    {
        $id = $array['id'];
        $group = $array['group'];
        echo "<option value = '$id'> $group </option>";
    }?>
</select><br>
Email*:
<input type = "text" name = "email" value = "<?php echo $user_data['email'];?>"><br>
<input type = "submit" name "update" value = "update">
</form>

以上代码是从数据库查询的更新表单。除组

外,所有变量都可以
   if(empty($_POST) === false && empty($error) === true)
    {
        $update_data = array(
            'std_id' => $_POST['std_id'],
            'name' => $_POST['name'],
            'surname' => $_POST['surname'],
            'group' => (int)$_POST['group'],
            'email' => $_POST['email'],
        );

        print_r($_POST);
                    print_r($update_data);

        die();
    }

这用于从提交按钮获取数据。我尝试使用print_r获取每个数组中的所有值。除了小组外,他们都有。我可以解决这个问题吗?

    Array ( [std_id] => 52211001 [name] => testname2 [surname] => testsurname1 [group] => [email] => tes21@test.com ) 
    Array ( [std_id] => 52211001 [name] => testname2 [surname] => testsurname1 [group] => 0 [email] => tes21@test.com )

这是一个输出

1 个答案:

答案 0 :(得分:0)

为什么要转换为int?这一行:

echo "<option value = '$id'> $group </option>";

应该是:

echo '<option value = "'.$id.'">'. $group .'</option>';