I have created a form with a drop-down element that reads a MySQL table to populate the options and when it writes the form data to a different field, it writes the row ID for the option selected. When I use only one element, it works as expected however when I copy the element it throws the below errors:
State
Warning: mysqli::query(): Couldn't fetch mysqli in /var/www/jobtrak/register.php on line 202
Warning: main(): Couldn't fetch mysqli in /var/www/jobtrak/register.php on line 209
User Type
Warning: mysqli::query(): Couldn't fetch mysqli in /var/www/jobtrak/register.php on line 217
Warning: main(): Couldn't fetch mysqli in /var/www/jobtrak/register.php on line 224
Here is the PHP/HTML code for the two elements. Line 202 is the 4th line, 209 is the 11th line, 217 is the 19th line and 224 is the 26th line.
<div class="form-group">
<label>State</label>
<?php
if($r_set1 = $link->query("SELECT * from state")){
echo "<select id=state name=state class=form-control style=width:150px>";
while ($row1 = $r_set1->fetch_assoc()) {
echo "<option value=$row1[id]>$row1[state]</option>";
}
echo "</select>";
}else{
echo $link->error;
}
?>
<span class="invalid-feedback"><?php echo $state_err; ?></span>
</div>
<div class="form-group">
<label>User Type</label>
<?php
if($r_set2 = $link->query("SELECT * from usertype")){
echo "<select id=usertype name=usertype class=form-control style=width:150px>";
while ($row2 = $r_set2->fetch_assoc()) {
echo "<option value=$row2[id]>$row2[type]</option>";
}
echo "</select>";
}else{
echo $link->error;
}
?>
<span class="invalid-feedback"><?php echo $usertype_err; ?></span>
</div>
I have looked here however I cannot see it closing the connection too early, or I'm just plain blind and cannot see it.