Golf input scores and persistence in MySQL using PHP

I'm trying to create a Leaderboard site for a golf tournament, and the idea is for people to post ratings for each hole that will be added to the MySQL database, and then a separate score page will be displayed in the leaderboard. I am at the very beginning, but I am stuck in the fact that the user enters ratings.

My thinking is to store grades in a table with columns for the golfer, hole_num, and hole_score.

I am currently using this in my HTML to enter ratings.

<form action="insert.php" method="POST">     
Golfer: <input type="text" name="golfer" />
<br>
Hole Number: <input type="number" name="hole_num" />
<br>
Hole Score: <input type="number" name="hole_score" />
<br>

<input type="Submit" value="Add"/></form>

insert.php:

<?php
include ('db_connect.php');

// Escape user inputs for security
$golfer = mysqli_real_escape_string($link, $_POST['golfer']);
$hole_num = mysqli_real_escape_string($link, $_POST['hole_num']);
$hole_score = mysqli_real_escape_string($link, $_POST['hole_score']);

// Attempt insert query execution
$sql = "INSERT INTO scores (golfer, hole_num, hole_score) VALUES('$golfer','$hole_num','$hole_score')";
mysqli_query($link, $sql);

// Close database connection
mysqli_close($link);

?>

, , "", "hole_num" "hole_score", . , 1 . , , , 18 , , , . , - , , db, golfer, hole_num hole_score. , . , , . , - .

+4
1

.

, , . , .

: .

:

<form action="insert.php" method="POST">
Golfer: <input type="text" name="golfer" />
<br>
<table>
<tr><th>Hole Number</th><th>Hole Score</th></tr>
<?php
for ($i = 1; $i <= 18; $i++): ?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="number" name="hole_score[<?php echo $i; ?>]" /></td>
</tr>
<?php endfor; ?>
</table>

<input type="Submit" value="Add"/></form>

:   

// Escape user inputs for security
$golfer = mysqli_real_escape_string($link, $_POST['golfer']);

$values = [];
foreach ($_POST['hole_score'] as $key => $score) {
    $score = trim(mysqli_real_escape_string($link, $score));
    if (!empty($score)) {
        $values[] = "('$golfer','$key','$score')";
    }
}

if (!empty($values)) {
    $values = implode (' , ', $values);

    // Attempt insert query execution
    $sql = "INSERT INTO scores (golfer, hole_num, hole_score) VALUES $values";
    mysqli_query($link, $sql);
}
// Close database connection
mysqli_close($link);

?>
+1

Source: https://habr.com/ru/post/1664722/


All Articles