PHPスクリプトを使用してmySQLの値を更新しようとすると、いくつかの問題が発生します。次の表の行を次のコードで更新しようとしました(ユーザー "Bob"は得られた合計スコアとパーセンテージを計算するための値を既に入力していますが、test3グレードは以前の値の90ではなく100に更新する必要があります)。
<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>
しかし、それが得られた合計との割合を再計算するために、以前のテストの点数を引くない、90の以前のスコアから100までのテストスコアと試験3を更新します。その結果、得られた合計とパーセントが0に更新されます。私がmySQLとPHPをかなり新しくしているので、助けてくれれば幸いです。ありがとう!
前の表:UPDATE文を使用して
+----+-------+-------+-------+-------+----------------+-------------+---------+
| id | name | test1 | test2 | test3 | totalobtained | totalmarks | percent |
+----+-------+-------+-------+-------+----------------+-------------+---------+
| 7 | Bob | 100 | 100 | 90 | 290 | 500 | 96 |
+----+-------+-------+-------+-------+----------------+-------------+---------+
更新表:
+----+-------+-------+-------+-------+----------------+-------------+---------+
| id | name | test1 | test2 | test3 | totalobtained | totalmarks | percent |
+----+-------+-------+-------+-------+----------------+-------------+---------+
| 7 | Bob | 100 | 100 | 100 | 0 | 500 | 0 |
+----+-------+-------+-------+-------+----------------+-------------+---------+
あなたのコードは[** SQLインジェクション**](https://en.wikipedia.org/wiki/SQL_injection)攻撃の脆弱性があります。 [** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https://secure.php.net/)を使用してください。 manual/en/pdo.prepared-statements.php)は、[** this post **](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql - インジェクション - イン - php)。 –
GET要求の場合は、このコマンドは、 '$ sql = "UPDATE table1 SET test3 =' 100 '、totalobtained =' $ acquired '、 percent =' $ percentage 'WHERE name =' Bob '"; &%はまだ初期化されていないため0になります。何が必要ですか? –
@AgamBangahmm。それは私が立ち往生している場所です。得られた合計とパーセントを$ test1、$ test2、および更新した$ test3をその計算に含めるにはどうすればよいですか?私はmySQLとPHPに新しいので、私はこれをどうすればいいのか分かりません。 – AmateurCoder