I'm trying to create a way for a user to submit team scores through an HTML form, and store those values in a MySQL table to read back later.
The issue (besides that I am finding random code online) is that I need each instance of the score (Score1, Score2, Score3, etc) to just update its value. So if a new score is added for "Score1" it should replace the old score. Right now it's just creating rows with each new submission listed.
So here is what I was able to put together so far. This is the form that will be duplicated roughly 200 times (I have 200 scores to insert):
<form action="storage.php" id="scores" method="post">
<input type="number" placeholder="00" maxlength="2" name="score" id="score1">
Then my storage.php file looks like this:
<?php
$host = "localhost"; // Host name
$username = "scoreboard"; // Mysql username
$password = "XXXX"; // Mysql password
$db_name = "admin_scoreboard"; // Database name
$tbl_name = "scores"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
// Get values from form
$score1 = $_POST['score1'];
// Insert data into mysql
$sql = "INSERT INTO $tbl_name(score1)VALUES('$score1')";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if ($result) {
echo "Successful";
echo "<BR>";
echo "<a href='test.html'>Back to main page</a>";
} else {
echo "ERROR";
}
// close connection
mysql_close();
?>
In case this is confusing, my overall goal is to have coaches submit scores for their teams through an HTML form, store that in MySQL, and then display the scores in an HTML table late. But yeah, the main thing is to have the coaches go back and update the scores for the particular team if needed. I'll worry about pulling the data back from MySQL at another time :)