KDG
KDG

Reputation: 15

Invalid argument supplied for foreach() - Yet output is showing

I am working to show editable fields based on query results. I know the query is functioning properly, and it is returning an array. The array is populating the form fields properly, however, I am getting the "Invalid argument supplied for foreach()" warning. I am new at this, and at a loss as to what is happening. I appreciate any suggestions.

Here is the code:

// 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>

Upvotes: 1

Views: 274

Answers (3)

TameRobots
TameRobots

Reputation: 156

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'];

Upvotes: 2

Ray
Ray

Reputation: 41428

You're not changing the row item, so don't pass by reference to the foreach. Also, shouldn't you be using $item instead of $row? Do this:

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

Don't do This:

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

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57322

foreach($row as &$item) should be

 foreach($row as $item)

there is no need to use the foreach here you can just do this like like

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

$record = $row['RECORD'];
$school = $row['SCHOOL'];
$type = $row['TYPE'];
$degree = $row['DEGREE'];
$major = $row['MAJOR'];
$grad = $row['GRAD'];
}

Upvotes: 1

Related Questions