[英]Multiple update queries using loop in PHP & MySQL
I have a table wherein I store the list of students enrolled in a class, and also their respective grades. 我有一个表格,其中存储了课程中所注册学生的列表以及他们各自的成绩。 It has STUD_ID and STUD_GRADE as columns, and the default value of the STUD_GRADE is NA. 它具有STUD_ID和STUD_GRADE作为列,并且STUD_GRADE的默认值为NA。
What I want to do is to display all the students in the table, and a textbox for their grades beside them, and when the submit button is pressed, then all of the rows would be updated. 我想做的是显示表格中的所有学生,以及旁边显示其成绩的文本框,并且当按下“提交”按钮时,所有行都将被更新。
I display them all by using this code: 我通过使用以下代码显示它们:
$query = "SELECT stud_id FROM tblgrade";
$result = mysql_query($query,$db) or die (mysql_error($db));
while($row = mysql_fetch_array($result))
{
extract($row);
echo "<tr><td><input type='hidden' value='$stud_id' name='id'/> stud_id </td>
<td><input type='text' size='10' name='grade'></td>
<tr>";
}
?>
What I am trying to do is to update the NA grades by using a loop in either the query itself or PHP functions. 我正在尝试通过在查询本身或PHP函数中使用循环来更新NA等级。 I tried so many things already but I'm really having difficulties in making the proper loop. 我已经尝试了很多事情,但是在制作正确的循环时确实遇到了困难。 It always give out a result wherein only the last row would be updated. 它总是给出只更新最后一行的结果。
Here is the first and last thing I tried to work on: 这是我尝试的第一件事也是最后一件事:
$query = "SELECT stud_id FROM tblgrade";
$result = mysql_query($query,$db) or die (mysql_error($db));
while($row = mysql_fetch_array($result))
{
$query1 = "UPDATE tblgrade SET stud_grade = '$_POST[grade]' WHERE stud_id = '$_POST[id]'";
mysql_query($query1,$db) or die (mysql_error($db));
}
echo "Grades have been updated!";
I choose not to post the loops I tried, because they are all really messed up, and I am hoping something like the one I posted above can be done. 我选择不发布尝试过的循环,因为它们确实很混乱,我希望可以完成上面发布的循环。
Suggestions, anyone? 建议,有人吗? Thanks in advance. 提前致谢。 :D :D
PS: Really not good at looping.. :(
PS:真的不擅长循环.. :(
i would do it like this: 我会这样做:
UPDATE tblgrade
SET stud_grade = CASE
WHEN stud_id_1_value THEN 'stud_grade_1_value'
WHEN stud_id_2_value THEN 'stud_grade_2_value'
WHEN stud_id_3_value THEN 'stud_grade_3_value'
...
END
WHERE stud_id IN (stud_id_1_value,stud_id_2_value,stud_id_3_value,...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.