iT邦幫忙

0

關於for迴圈取得form裡面POST的資料(更*找到錯誤了,謝謝)

不明 2021-12-30 12:32:182150 瀏覽
  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20211230/20145270a6wTk8ohRY.png
不好意思,想請問一下,
這是我想做的表單,我想要輸入成績之後存到欄位對應的學號,
但不知道為甚麼只會存到第一筆 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取代掉了
已解決

看不出你的POST能針對每個學生的成績做處理
不明 檢舉
我用學號當作分數的name,存在$score[$i]的矩陣裡面,在if(isset($_POST['cID']))前面測試的時候$score[$i]裡面存的是 S0003 跟 S0004,但進入for迴圈裡面的時候,$score[$i]裡面存的數值變成 S0003 和 0 了,我不知道為什麼,哪裡做錯了QQ"
Felix iT邦研究生 2 級 ‧ 2021-12-30 17:59:31 檢舉
請問「不明」是怎麼回事?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-12-30 15:13:06

陣列的名稱只要設為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

資料來源

/images/emoticon/emoticon55.gif

快問快答快閃
真是好快的身手
/images/emoticon/emoticon29.gif

我要發表回答

立即登入回答