The Scenario
I have a scoring card that is populated from another table in the MTSQL with an unknown amount of players. each week the players needs to be scored and those scores needs to be input into the mySQL.
The dynamic html table create for the Mysql table call member.
<form method="post" action="scores-add" id="member-add-form" enctype="multipart/form-data">
<div class="table-responsive">
<table id="data-table-1" class="table table-striped table-bordered">
<thead>
<tr>
<th class="align-middle">Name</th>
<th class="align-middle">Team</th>
<th class="align-middle">Week</th>
<th class="align-middle">Score 1</th>
<th class="align-middle">Score 2</th>
<th class="align-middle">Score 3</th>
<th class="align-middle">Score 4</th>
<th class="align-middle">Score 5</th>
<th class="align-middle">Score 6</th>
<th class="align-middle">Score 7</th>
<th class="align-middle">Score 8</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM member ORDER BY member_name ASC";
$result = mysqli_query($db,$sql) or die("Database access failed: " . mysqli_error());
while ($team = mysqli_fetch_assoc($result)) {
?>
<tr >
<td class="align-middle"><input type="text" name="scoring_1" value="<?php echo $team["member_name"]; ?>" readonly hidden> <?php echo $team["member_name"]; ?></td>
<td class="align-middle"><input type="text" name="scoring_2" value="<?php echo $team["member_team"]; ?>" readonly hidden> <?php echo $team["member_team"]; ?></td>
<td class="align-middle"><input type="text" name="scoring_3" value="" readonly hidden> </td>
<td class="align-middle"><input type="checkbox" name="scoring_4" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_5" value="10"></td>
<td class="align-middle"><input type="checkbox" name="scoring_6" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_7" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_8" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_9" value="5"></td>
<td class="align-middle"><input type="checkbox" name="scoring_10" value="20"></td>
<td class="align-middle"><input type="checkbox" name="scoring_11" value="50"></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<div class="form-row mb-3">
<button type="submit" class="btn btn-primary btn-block" name="score_add_btn">Add Score</button>
</div>
</form>
The function to add the table to the SQL
/ call the team_edit() function if team_edit_btn is clicked
if (isset($_POST['score_add_btn'])) {
score_add();
}
// LOGIN USER
function score_add(){
global $db, $errors, $member_id, $member_name_edit, $member_team_edit, $members_view_url;
// grap form values
$scoring_1 = $_POST['scoring_1'];
$scoring_2 = $_POST['scoring_2'];
$scoring_3 = $_POST['scoring_3'];
$scoring_4 = $_POST['scoring_4'];
$scoring_5 = $_POST['scoring_5'];
$scoring_6 = $_POST['scoring_6'];
$scoring_7 = $_POST['scoring_7'];
$scoring_8 = $_POST['scoring_8'];
$scoring_9 = $_POST['scoring_9'];
$scoring_10 = $_POST['scoring_10'];
$scoring_11 = $_POST['scoring_11'];
// make sure form is filled properly
if (empty($scoring_1)) {
array_push($errors, "Name is required");
}
if (count($errors) == 0) {
$query = "INSERT INTO scoring (scoring_1, scoring_2, scoring_3, scoring_4, scoring_5, scoring_6, scoring_7, scoring_8, scoring_9, scoring_10, scoring_11 )
VALUES ('$scoring_1', '$scoring_2', '$scoring_3', '$scoring_4', '$scoring_5', '$scoring_6', '$scoring_7', '$scoring_8', '$scoring_9', '$scoring_10', '$scoring_11')";
mysqli_query($db, $query);
$_SESSION['success'] = "<h4>Done</h4>";
header("location: $leaderboard_menmber_url");
}else {
$_SESSION['failure'] = "Error updating record: " . $db->error;
header("location: $leaderboard_menmber_url");
}
$db->close();
}
The Problem
The function only adds the last line from the dynamic html table to the database.
The desired outcome.
Each entry in the dynamic HTML table needs to be added to the MySQL as its own entry.