之前寫網站的時候真的沒用過 form傳陣列的方式
用陣列的方式就可以將欄位的值記錄在陣列裡讓後端php (在input type name那加陣列 [])
可以跑多值的mysql修改
以下是程式碼
<前端>
<form action="testArray.php" name="form2" method="post">
<input type="submit" value="送出表單">
<ul class="accordionPart">
<li>
<div class="qa_title" style="text-decoration:none;">課表 ▾</div>
<?php $data = mysql_query("select * from Course_Taught where Professor_Id='A112'", $conn); ?>
<div class="qa_content">
<table width="790" bgcolor="black" style="font-size:15px">
<tr>
<td colspan="1" bgcolor="#e3e3e3"><b>ID</b></td>
<td colspan="1" bgcolor="#e3e3e3"><b>Program</b></td>
<td colspan="1" bgcolor="#e3e3e3"><b>Academic Year</b></td>
<td colspan="1" bgcolor="#e3e3e3"><b>Semester</b></td>
<td colspan="1" bgcolor="#e3e3e3"><b>Course Title</b></td>
<td colspan="1" bgcolor="#e3e3e3"><b>Credit hour</b></td>
</tr>
<?php
for($i=1; $i<=mysql_num_rows($data); $i++){ //把每一列的資料取出來
$rs=mysql_fetch_row($data);
?><tr>
<td colspan="1" bgcolor="#FFFFFF">
<input type="text" name="course_id[]" value="<?php echo $rs[0] ?>"
style="width:100px; font-size:15px; text-align:center;
text-overflow:ellipsis; overflow: hidden;"/>
</td>
<td colspan="1" bgcolor="#FFFFFF">
<input type="text" name="program[]" value="<?php echo $rs[1] ?>"
style="width:100px; font-size:15px; text-align:center;
text-overflow:ellipsis; overflow: hidden;"/>
</td>
<td colspan="1" bgcolor="#FFFFFF">
<input type="text" name="academic_year[]" value="<?php echo $rs[2] ?>"
style="width:100px; font-size:15px; text-align:center;
text-overflow:ellipsis; overflow: hidden;"/>
</td>
<td colspan="1" bgcolor="#FFFFFF">
<input type="text" name="semester[]" value="<?php echo $rs[3] ?>"
style="width:100px; font-size:15px; text-align:center;
text-overflow:ellipsis; overflow: hidden;"/>
</td>
<td colspan="1" bgcolor="#FFFFFF">
<input type="text" name="course_title[]" value="<?php echo $rs[4] ?>"
style="width:100px; font-size:15px; text-align:center;
text-overflow:ellipsis; overflow: hidden;"/>
</td>
<td colspan="1" bgcolor="#FFFFFF">
<input type="text" name="credit_hour[]" value="<?php echo $rs[5] ?>"
style="width:100px; font-size:15px; text-align:center;
text-overflow:ellipsis; overflow: hidden;"/>
</td>
</tr>
<?php }?>
</table>
</div>
</li>
</ul>
</form>
php端
<?php
//include_once("connection.php");
session_start();
//學年度授課
//$course_id = $_POST['course_id'];
$course_id =($_POST['course_id']);
$program = ($_POST['program']);
$academic_year = $_POST['$academic_year'];
$semester = $_POST['semester'];
$course_title = $_POST['course_title'];
$credit_hour = $_POST['credit_hour'];
$_SESSION['program'] = $program;
$_SESSION['course_id'] = $course_id;
echo '<br>';
print_r($_POST['course_id']); //測試後端是否有收到陣列 會印出來
//Sql_Update
$con=mysqli_connect("sever位置","使用者","密碼","資料庫");
$sql="select * from Course_Taught where Professor_Id='A112'";
$result=mysqli_query($con,$sql);
// 檢測連接
if (mysqli_connect_errno())
{
echo "連接失敗: " . mysqli_connect_error();
}
mysqli_query($con, "set names utf8"); //utf8 設為對應的編碼 超級重要
for($i=0; $i<mysqli_num_rows($result); $i++){ //把每一列的資料取出來
//傳到sql指令時記得 $academic_year[$i] 變數裡的陣列i 也要打上$ 如此就可以多值傳送到資料庫做修改的動作
$sql_Course_Taught = "UPDATE Course_Taught SET Program = '$program[$i]',Academic_Year = '$academic_year[$i]',Semester = '$semester[$i]',Course_Title = '$course_title[$i]',Credit_Hour = '$credit_hour[$i]' WHERE Id = '$course_id[$i]'";
mysqli_query($con,$sql_Course_Taught);
echo '<br>';
echo $i;
echo '<br>';
echo $course_id[$i];
echo '<br>';
echo $program[$i];
echo '<br>';
}
mysqli_close($con);
header("location: test.php");
?>
留言列表