不好意思,想請問一下,
這是我想做的表單,我想要輸入成績之後存到欄位對應的學號,
但不知道為甚麼只會存到第一筆 S0003 的成績, S0004 不會存到。
請問我該如何解決這個問題?
這是我目前的程式碼:
<?php
session_start();
$id=$_SESSION['user'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "teacher";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM 課程表 WHERE 任課老師='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<form method='GET'>";
echo "<select name = '課號'>";
echo "<option disabled>請選擇課號</option>";
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row["課號"]."</option>";
}
echo "</select>";
echo "<input type='submit' value='確認'>";
echo "</form>";
}
else {
echo "<p>尚未教授任何課程</p>";
}
mysqli_close($conn);
?>
<?php
if(isset($_GET['課號'])){
$cID=$_GET['課號'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM `成績表` WHERE `課號` = '$cID'";
$result = mysqli_query($conn, $sql);
$result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
echo "<form method='POST'>";
echo "<br><TABLE BORDER='1'>";
echo "<TR ALIGN='CENTER'>";
echo "<th>課號</th>";
echo "<th>學號</th>";
echo "<th>成績</th>";
echo "</TR>";
$count=0;
while($row = mysqli_fetch_assoc($result)){
echo "<TR ALIGN='CENTER'>";
echo "<TD>".$row["課號"]."</TD>";
echo "<TD>".$row["學號"]."</TD>";
$score[$count]=$row["學號"];
echo "<input type='hidden' size=10 name='cID' required='required' value='$cID'/>";
echo "<TD><input type='text' name='$score[$count]' value='".$row["分數"]."' size=1/></TD>";
echo "</TR>";
$count++;
}
echo "</table><br>";
echo "<input type='submit' value='送出'/>";
echo "</form>";
}
else{
echo "<p>還沒有任何學生修該堂課程!</p>";
}
mysqli_close($conn);
}
?>
<?php
if(isset($_POST['cID'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$cID=$_POST['cID'];
for($i=0;$i<$count;$i++){
if(isset($_POST[$score[$i]])){
$sID = $score[$i];
$score = $_POST[$sID];
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE `成績表` SET `分數` = '$score' WHERE `課號` = '$cID' AND `學號` = '$sID'";
$result = mysqli_query($conn, $sql);
$result=$conn->query($sql);
}
}
echo '<meta http-equiv="refresh" content="0;">';
mysqli_close($conn);
}
?>
更新:
找到錯誤了QQ"
我耍蠢了$score = $POST[$sID]這段把$score取代掉了
已解決
陣列的名稱只要設為score[]
即可
範例如下
HTML
<form action="action.php" method="post">
<input type="checkbox" name ="colddrinks[]" value="Coke"> Coke <br>
<input type="checkbox" name ="colddrinks[]" value="Fanta"> Fanta <br>
<input type="checkbox" name ="colddrinks[]" value="Sprite"> Sprite <br>
<input type="submit" value="Submit">
</form>
PHP
$coldDrinks = $_POST['colddrinks'];
foreach ($coldDrinks as $coldDrink){
echo $coldDrink."<br/>";
}
Output:
Coke
Fanta