Search code examples
phphtmlsinglestore

Inserting data from database into table. LAST COLUMN NOT WORKING


My code is working correctly except the last table data (data 7) is not inserting and showing up blank.

<table id="History" class="table table-striped table-bordered" style="width:100%">
          <a href="export.php"> Export To Excel </a> 

    <thead>
        <tr>
            <th>First <br> Name</th>
            <th>Last <br> Name</th>
            <th>Competition <br> Name</th>
            <th>Competition Level</th>
            <th>Dance Name</th>
            <th>Number of Competitors</th>
            <th>Dancer's Placement</th>
            <th>Dancer's Score</th>
            <th>1st Place Score</th>
        </tr>
    </thead>
          <?php
          $sql = "SELECT `dancer_name`,`dancer_lastname`,`comp_name`,`competition_level1`, `dance_name1`, `number_competitors1`, `dancer_placement1`, `dancer_score1`, `1stpl_score1` FROM `report` WHERE name = '$name'";
          $res = mysqli_query($con,$sql); 

          //if($res==FALSE){
          //die('there was an error running query [' . $con->error . ']');
          //  }

          while($data=mysqli_fetch_row($res)){
              echo '
              <tr>
              <td>'.$data[0].'</td>
              <td>'.$data[1].'</td>
              <td>'.$data[2].'</td>
              <td>'.$data[3].'</td>
              <td>'.$data[4].'</td>
              <td>'.$data[5].'</td>
              <td>'.$data[6].'</td>
              <td>'.$data[7].'</td>
              </tr> 
              ';  
    }

?>  
</table>

data 0-6 shows up fine, but 7 is always blank. Please tell me, what's wrong with my code?


Solution

  • I first suggest you use php function var_dump($data) to shown content inside it. Later you have to check if index seven [7] is null or empty, if so check in your table database to make sure if record exist... If exist, check your query to make sure if properties are correct.

    This could helpful:

    Documentation PHP var_dump

    Bonus: I suggest you use mysqli_fetch_array instead mysqli_fetch_row, because you can "use associative indices, using the field names of the result set as keys", making your code more maintainable and legibility.

    For example:

    $data["dancer_name"]; // instead $data[0]