-1

I have a table within a form. Most of the fields are populated by data from the database via a loop. But then I have two input fields as well where the user will input some results, and then submit the form. On submission, the database should be updated with these results.

When I load the page, the table appears with all the data. However, above the table I am getting an error message "Notice: Undefined index:(the input box is shown here and not in the table!!) in C:\xampp\htdocs\testsite\test-predictions.php on line 37. It shows ten times, which is the two fields times the number of rows generated. This is the code:

<?php
include 'includes/dbh.inc.php';
?>

<div class="predictions_container">
<form class="predictions" name="predictions" action="test-predictions.php" method="post">
<table class="table">
<thead>
<tr class="active">
<th>Match ID</th>
<th>Match Date</th>
<th>Locked</th>
<th>Home Team</th>
<th>Home Score</th>
<th>Home Score</th>
<th>Away Score</th>
</tr>               
</thead>            
<?php
// Get data to fill table columns

$sql = "SELECT match_id, matchdate, locked, hometeam, awayteam from matches";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc()) {
        echo "<tr class='info'>
                <td>". $row["match_id"] ."</td>
                <td>". $row["matchdate"] ."</td>
                <td>". $row["locked"] ."</td>
                <td>". $row["hometeam"] ."</td>
                <td>". $row["awayteam"] ."</td>
                <td>". $row["<input type='text' name='homescore'/>"] ."</td>
                <td>". $row["<input type='text' name='awayscore'/>"] ."</td>
              </tr>";
    }
} else {
    echo "No results";
}
$conn-> close();
?>

</table>        
<td><input class="submit" type="submit" size="30" value="Submit" /></td>        
</form>
</div>

<?php

Can someone please tell me how I can fix it? I'm still quite a beginner in PHP. Thanks.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
jbm59er
  • 35
  • 1
  • 3

2 Answers2

3

The line with with the problem is this:

<td>". $row["<input type='text' name='homescore'/>"] ."</td>

Change it to:

<td><input type='text' name='homescore'/></td>

Just a little syntax mixup.

David
  • 344
  • 1
  • 9
0

You are trying to access the indexes:

input type='text' name='homescore'

input type='text' name='awayscore'

which are undefined

Community
  • 1
  • 1
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63