0

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.

  • There are some reasonable answers here: https://stackoverflow.com/questions/19937880/mysqliquery-couldnt-fetch-mysqli/26147425 – Bill Karwin Apr 25 '21 at 03:07
  • @BillKarwin that's the article I linked to in my original question. – Christopher H Apr 25 '21 at 03:10
  • Apologies, I did not check that. I would debug your code by making sure `$link` is an active connection. Query `SELECT true` or something, and check for [error status](https://www.php.net/mysqli_error). – Bill Karwin Apr 25 '21 at 06:15
  • No dice, `$link` is active connection, confirmed by using `SELECT true`. Still presenting the same issue. – Christopher H Apr 25 '21 at 06:48

1 Answers1

0

try wrapping HTML Tag Attribute in quotation marks " ". And/or $row1['id'], $row1['state']

eg.

<?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>";
        }