1

我正在根据查询结果显示可编辑字段。我知道查询运行正常,并且它返回一个数组。该数组正在正确填充表单字段,但是,我收到“为 foreach() 提供的参数无效”警告。我对此很陌生,对正在发生的事情一无所知。我很感激任何建议。

这是代码:

// Grab the profile data from the database
$query8 = "SELECT * FROM EDUCATION WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' ORDER BY RECORD";
$data = mysqli_query($dbc, $query8);

echo '<pre>' . print_r($data, true) . '</pre>'; 
$rowcount = 1;
while ($row = mysqli_fetch_assoc($data))
{
    if (is_array($row))
    {
        echo '<p> It is an Array</p>';
    }
    foreach($row as &$item)
    {
        $record = $row['RECORD'];
        $school = $row['SCHOOL'];
        $type = $row['TYPE'];
        $degree = $row['DEGREE'];
        $major = $row['MAJOR'];
        $grad = $row['GRAD'];

        ?>
        <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

        <fieldset>
        <legend>Education History </legend>
        <?php
        echo '<input type="hidden" id="record" name="record" value="' . $record . '">';
        echo 'Rowcount' . $rowcount. '</br>';
        // Insert Listbox here
        $queryschool = "SELECT * FROM SCHOOL";
        $list = mysqli_query($dbc, $queryschool);
        if($list) 
        {
            echo 'School Type? ';
            echo '<select name="school_code">';
            while($row = mysqli_fetch_assoc($list))
            {
                echo "<option value={$row['CODE']}>{$row['TYPE']}" ;
                echo '</option>';
            }
            echo '</select>';
        }

        echo '<br />';
        echo '<label for="school">School Name:</label>';
        echo '<input type="text" id="school" name="school" size="40" maxlength="40" value="' . ( (!empty($school)) ? $school : "") . '" /><br />';

        // Insert Listbox here
        $querydegree = "SELECT * FROM DEGREE";
        $list = mysqli_query($dbc, $querydegree);
        if($list) 
        {
            echo 'Degree Type? ';
            echo '<select name="degree_code">';
            while($row = mysqli_fetch_assoc($list))
            {
                echo "<option value={$row['CODE']}>{$row['DEGREE']}";
                echo '</option>';
            }
            echo '</select>';
        }
        echo '<br />';
        echo '<label for="major">Field of study:</label>';
        echo '<input type="text" id="major" name="major" size="40" maxlength="40" value="' . ( (!empty($major)) ? $major : "") . '" /><br />';
        echo '<label for="grad">Did you graduate?:</label>';
        echo '<input type="radio" id="grad" name="grad" value="Y" ' . ($grad == "Y" ? 'checked="checked"':'') . '/>Yes ';
        echo '<input type="radio" id="grad" name="grad" value="N" ' . ($grad == "N" ? 'checked="checked"':'') . '/>No<br />';
        ?>
        </fieldset>
        <?php
        $rowcount++;
    }
}
;
echo '<label for="another">Do you need to enter more educational experience?:</label>';
echo '<input type="radio" id="another" name="another" value="Y" ' . ($another == "Y" ? 'checked="checked"':'') . '/>Yes ';
echo '<input type="radio" id="another" name="another" value="N" ' . ($another == "N" ? 'checked="checked"':'') . '/>No<br />';

?>

<input type="submit" value="Save Profile" name="submit" />
</form>
4

3 回答 3

2
foreach ($row as &$item) 

replace this with:

foreach ($row as $item)

And then for each variable you should probably change

$record = $row['RECORD'];

to

$record = $item['RECORD'];
于 2012-11-15T15:30:17.807 回答
1

foreach($row as &$item)应该

 foreach($row as $item)

没有必要在这里使用 foreach 你可以这样做

while ($row = mysqli_fetch_assoc($data))
{

$record = $row['RECORD'];
$school = $row['SCHOOL'];
$type = $row['TYPE'];
$degree = $row['DEGREE'];
$major = $row['MAJOR'];
$grad = $row['GRAD'];
}
于 2012-11-15T15:26:43.513 回答
0

您没有更改行项目,因此不要通过引用传递给 foreach。另外,您不应该使用 $item 而不是 $row 吗?做这个:

foreach($row as $item)
 {
  $record = $item['RECORD'];
  $school = $item['SCHOOL'];
  ....

不要这样做:

foreach($row as &$item)
  {
  $record = $row['RECORD'];
  $school = $row['SCHOOL'];
  ....
于 2012-11-15T15:28:49.810 回答