当从sql表中提取变量时,在数组中使用变量

时间:2010-09-01 23:53:37

标签: php sql forms

基本上我正在尝试创建一个允许用户编辑表中数据的简单表单。它从数据库中提取数据并使用这些变量预填充表单。然后用户可以只编辑表单中的信息并重新提交数据。问题是......状态选择是一个函数,它自动创建填充所有状态的表单中的下拉框。我想如果我只是在该状态数组的开头添加一个变量,那么数组中的第一个选项将是已经存在于数据库中的状态,但它不起作用 - 它只是空白。 (下拉列表中显示的第一个选项为空白,状态列表的其余部分确实显示应该显示)

 while ($row = mysql_fetch_array($result)) {
      $id = $row['id'];
      $first = $row['first'];
      $last = $row['last'];
      $city = $row['city'];
      $state = $row['state'];
      $email = $row['email'];
      $bday = $row['bday'];

        function state_selection()
        {
            $str = '<select name="state2">';
            $states = array(
                    "$state","Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii",                                                                                                   "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",                                                                            "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",                                                                            "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia",                                                                            "Wisconsin", "Wyoming"
                );
            foreach ($states as $state_name)
            {
                $str .= '<option value="'.$state_name.'">'.$state_name.'</option>';
            }
            $str .= '</select>';

            return $str;
        }

      $editBlock = '<form method="post" action="./sadd.php">
      <p>First Name: <input name="first2" value="' . $first . '" type="text"  size=13 maxlength=25 /></p>
      <p>Last name: <input name="last2" value="' . $last . '" type="text" size=13 maxlength=25 /></p>
      <p>City:<input name="city2" value="' . $city . '" type="text" size=25 maxlength=50 /></p>
      <p>State: '.state_selection().' </p>
      <p>Email:<input name="email2" value="' . $email . '" type="text" size=50 maxlength=75 /></p>
      <p>Birthday:<input name="bday2" value="' . $bday . '" type="text" size=10 maxlength=10 />(ex 1982-06-26)</p>
      <p><input type="hidden" name="eb" value="ebf" /></p>
      <p><input type="submit" name="submit" value="Update Contact" /></p>
      </form>';

      echo "$editBlock"; }

3 个答案:

答案 0 :(得分:3)

$ state不在范围内,改为

 function state_selection($state){

并打电话给

 state_selection($state);

更可靠的方法,而不是预先添加

'<option value="'.$state_name.'" '.($state_name==$state)?'selected':'').'>'

答案 1 :(得分:1)

您无法在函数中访问变量$ state而不将其声明为全局变量。将此行添加到state_selection()

的开头
global $state;

另一种选择是传递$state作为state_selection()

的参数

答案 2 :(得分:0)

这是一些更新的代码。一些关键的注意事项:

  • 如果您只拉1条记录,则无需循环播放。
  • 函数内部的数组是不必要的。我不喜欢全局变量,因此我将其设置为传递参数。
  • 添加了默认参数,这会将所选状态设置为该参数。
  • 删除了将数组值分配给自己的变量。这不是必要且效率低下的。最好通过阵列访问它们。

有任何问题让我知道。

function state_selection($states, $default) {
    $str = '<select name="state2">';

    foreach ($states as $state_name) {
        $str .= '<option value="'.$state_name.'" ' . (($default == $state_name)?"selected":"") . '>'.$state_name.'</option>';
    }

    $str .= '</select>';

    return $str;
}

$states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", 
                    "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", 
                    "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", 
                    "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", 
                    "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", 
                    "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", 
                    "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");

$row = mysql_fetch_assoc($result);

$editBlock = '<form method="post" action="./sadd.php"> 
      <p>First Name: <input name="first2" value="' . $row['first'] . '" type="text"  size=13 maxlength=25 /></p>
      <p>Last name: <input name="last2" value="' . $row['last'] . '" type="text" size=13 maxlength=25 /></p>
      <p>City:<input name="city2" value="' . $row['city'] . '" type="text" size=25 maxlength=50 /></p>
      <p>State: ' . state_select($states, $row['state']) . ' </p>
      <p>Email:<input name="email2" value="' . $row['email'] . '" type="text" size=50 maxlength=75 /></p>
      <p>Birthday:<input name="bday2" value="' . $row['bday'] . '" type="text" size=10 maxlength=10 />(ex 1982-06-26)</p>
      <p><input type="hidden" name="eb" value="ebf" /></p>
      <p><input type="submit" name="submit" value="Update Contact" /></p>
      </form>';

echo $editBlock;