使用嵌套foreach循环这是我的代码:数据在更新查询
session_start();
/* loops through each row in the global $_SESSION variable which
contains the array and uses the $value to GET the data in the text
boxes and output them */
// studevent_result =
foreach ($_SESSION['arrayNameResult'] as $value) {
$studResult = $_GET[$value];
echo $studResult;
echo "<br>";
}
// result_postion =
foreach ($_SESSION['arrayNamePosition'] as $value) {
$studPosition = $_GET[$value];
echo $studPosition;
echo "<br>";
}
echo "<br>";
// stud_id =
foreach ($_SESSION['arrayId'] as $value) {
echo $value;
echo "<br>";
}
// UPDATE query, this will update the studevent_result and result_position
// column in the database for the specific stud_id.
$updateQuery = "
UPDATE result
SET studevent_result = '00:20:33',
result_position = '6'
WHERE result.stud_id = '12'
";
$updateRow = mysqli_query($conn, $updateQuery);
我用$ _SESSION变量,所有的存储阵列。我使用foreach循环提取这些数组的结果。
在$ updateQuery中,我想让studevent_result =我上面的第一个foreach循环的结果,result_position =上面的第二个foreach循环的结果,result.stud_id =上面的第三个foreach循环的结果。
我编辑的代码现在我的代码看起来像这样经过:
foreach ($_SESSION['arrayNameResult'] as $value) {
$studResult = $_GET[$value];
foreach ($_SESSION['arrayNamePosition'] as $data) {
$studPosition = $_GET[$data];
foreach ($_SESSION['arrayId'] as $idValue) {
echo $idValue;
$updateQuery = "
UPDATE result
SET studevent_result = '$studResult',
result_position = '$studPosition'
WHERE result.stud_id = '$idValue'
";
$updateRow = mysqli_query($conn, $updateQuery);
}
}
}
我嵌套在foreach循环。但现在的问题是,对于嵌套循环中的最后一个foreach循环,查询中的$ idValue仅使用数组$ _SESSION ['arrayId']中的最后一个元素。我怎样才能解决这个问题来循环整个数组,以便查询使用数组中的所有值?
在此先感谢。
您的代码易受SQL注入攻击。请阅读[如何防止SQL注入在PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)的信息如何解决它。 –