I am having some trouble trying to update some values in mySQL with PHP script. When I try to update the following table row with the following code (the user "Bob" has already inputted values to calculate their total score obtained and percentage but his test3 grade has to be updated to 100 instead of the previous value of 90):
<html>
<head></head>
<body>
<form class="form" action="" method="post">
<?php
$mysqli = new mysqli('', '', '', '');
if(isset($_POST['calculate'])) {
$name = $_POST['name'];
$test1 = $_POST['test1'];
$test2 = $_POST['test2'];
$test3 = $_POST['test3'];
$obtained = ($test1 + $test2 + $test3);
$total = 300;
$percentage = round(($obtained/$total)*100);
$result = mysqli_query($mysqli, "INSERT INTO table1 (name, test1,
test2, test3, totalobtained, totalmarks, percent)
VALUES ('$name', '$test1', '$test2', '$test3',
'$obtained', '$total', '$percentage')");
}
$conn = mysqli_connect('', '', '', '');
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE table1 SET test3='100', totalobtained='$obtained',
percent='$percentage' WHERE name='Bob'";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
<table>
<tr>
<th>Name of Student*:</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>Test 1*:</th>
<td><input type="number" name="test1"></td>
</tr>
<tr>
<th>Test 2*:</th>
<td><input type="number" name="test2"></td>
</tr>
<tr>
<th>Test 3*:</th>
<td><input type="number" name="test3"></td>
</tr>
<tr>
<th>Total Marks Obtained:</th>
<td><?php if(isset($_POST['calculate'])) { echo "$obtained";}?>
</td>
</tr>
<tr>
<th>Total Marks:</th>
<td><?php if(isset($_POST['calculate'])) { echo "$total";}?>
</td>
</tr>
<tr>
<th>Percentage:</th>
<td><?php if(isset($_POST['calculate'])) { echo "$percentage",
'%';}?></td>
</tr>
<tr>
<th><input type="submit" name="calculate" value="Calculate"/>
</th>
</tr>
</table>
</form>
</body>
</html>
It updates test 3 with the test score to 100 from the previous score of 90, however, it doesn't pull the previous test scores to recalculate the total obtained and percentage. As a result, it updates total obtained and percent to 0. Some help would be appreciated as I am pretty new to mySQL and PHP. Thanks!
Previous Table:
+----+-------+-------+-------+-------+----------------+-------------+---------+
| id | name | test1 | test2 | test3 | totalobtained | totalmarks | percent |
+----+-------+-------+-------+-------+----------------+-------------+---------+
| 7 | Bob | 100 | 100 | 90 | 290 | 500 | 96 |
+----+-------+-------+-------+-------+----------------+-------------+---------+
Updated Table with UPDATE statement:
+----+-------+-------+-------+-------+----------------+-------------+---------+
| id | name | test1 | test2 | test3 | totalobtained | totalmarks | percent |
+----+-------+-------+-------+-------+----------------+-------------+---------+
| 7 | Bob | 100 | 100 | 100 | 0 | 500 | 0 |
+----+-------+-------+-------+-------+----------------+-------------+---------+