2012-11-15 61 views
1

我正在努力根據查詢結果顯示可編輯的字段。我知道查詢功能正常,並且它正在返回一個數組。該數組正確填充表單字段,但是,我得到「爲foreach()提供的無效參數」警告。我對此感到新奇,對於發生的事情感到不知所措。我很欣賞任何建議。爲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> 

回答

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']; 
} 
+0

該更改消除了錯誤消息。現在問題在於fieldset的每個實例中的單選按鈕都起着一個作用。在一行上選擇「是」取消選擇所有其他行。我的下拉框不是默認選擇先前選擇的項目,並且通過提交按鈕,不會保存更改。 – KDG

0

你沒有改變行項目,所以不要通過引用傳遞給foreach。另外,你不應該使用$ item而不是$ row?這樣做:

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

不這樣做:

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

當我使用$ item ['field']進行更改時,窗體的輸出會截斷該數據並將迭代次數相乘?我希望能夠通過表單更改數據。 – KDG

2
foreach ($row as &$item) 

與替換此:

foreach ($row as $item) 

然後爲每個變量你應該改變

$record = $row['RECORD']; 

$record = $item['RECORD'];