4
for ($key=0; $key < count($_POST['marks']); $key++) {
$from_marks = $_POST['from'][$key];
$get_marks = $_POST['marks'][$key];
//echo $from_marks." ";
if($get_marks > $from_marks){
// header("location: ../../pages/marks.php?over=err");
// break;
echo "Cant add more marks <br/>";
}
else{
echo $get_marks."<br/>";
$update_marks_query = $db->prepare(
"UPDATE sc_marks SET get_marks='"
.$get_marks
."' WHERE _sid='$sc_foreign_id' AND exam_type='$select_exam_type' ");
$update_marks_query -> execute();
}
}
當我執行代碼時,發生了這個問題,我得到了表中每一行的最後一個提取值。更新後如何使用數組更新表列並在php和mysql中循環?
數據結果:
事實上,所有行最終都包含相同的數據,這表明您的UPDATE語句的WHERE子句總是匹配表中的每一行。如果不知道在哪裏以及如何定義已使用變量$ sc_foreign_id和$ select_exam_type,以及它們相關字段包含在數據庫中的數據類型,我無法給出更準確的答案。 – Thernys
順便說一句,你應該在'for'循環之前準備你的update語句'$ query = $ db-> prepare(「UPDATE sc_marks SET get_marks =?WHERE _sid =?AND exam_type =?」);'然後附加使用'$ query-> execute($ get_marks,$ sc_foreign_id,$ select_exam_type)''進行每次迭代時的參數。除了效率低下之外,您目前的方法存在安全風險。閱讀[SQL注入](http://www.unixwiz.net/techtips/sql-injection.html)。 – Thernys