使用PHP循环更新记录

使用PHP循环更新记录,php,mysqli,Php,Mysqli,我想使用php循环更新数据库中的数据 我已尝试更新数据,但它只更新列表中的最后一条记录,并将所有记录返回为null/zero // attempting to update data $rcount_student_lists = mysqli_query($mysqli, $count_student_lists); while($row2 = mysqli_fetch_row($rcount_student_lists)) $student_count_count = $row2['

我想使用php循环更新数据库中的数据

我已尝试更新数据,但它只更新列表中的最后一条记录,并将所有记录返回为null/zero

// attempting to update data
$rcount_student_lists = mysqli_query($mysqli, $count_student_lists);
while($row2 = mysqli_fetch_row($rcount_student_lists))
    $student_count_count = $row2['0'];

for ($id = 1; $id <=$student_count_count; $id++)
{  
    $sql = "UPDATE exam_data SET score_s = '".${'dd_'.$id}."' WHERE exam_name_s = '".$one."'";
}

if (mysqli_query($mysqli, $sql)) {
    echo juuhead("DETAILS UPDATED SUCCESFULLY");
} else {
    echo "Error updating record: " . mysqli_error($mysqli);
}
//正在尝试更新数据
$rcount\u student\u list=mysqli\u query($mysqli,$count\u student\u list);
而($row2=mysqli\u fetch\u行($rcount\u student\u list))
$student_count_count=$row2['0'];

对于($id=1;$id您正在循环中生成SQL字符串:

for ($id = 1; $id <=$student_count_count; $id++)
{  
    $sql = ...;
}
在循环内移动查询命令:

if (mysqli_query($mysqli, $sql)) {
for ($id = 1; $id <=$student_count_count; $id++)
{  
    $sql = ...
    if (mysqli_query($mysqli, $sql)) {
        ...
    } else {
        ...
    }
}
如果没有大括号,则while仅循环其后面的一行。若要循环多行,需要将这些行用大括号括起来:

while($row2 = mysqli_fetch_row($rcount_student_lists))
{
    $student_count_count = $row2['0'];
    for ($id = 1; $id <=$student_count_count; $id++)
    {
        ...
    }
}
while($row2=mysqli\u fetch\u row($rcount\u student\u list))
{
$student_count_count=$row2['0'];

对于($id=1;$id您正在循环中生成SQL字符串:

for ($id = 1; $id <=$student_count_count; $id++)
{  
    $sql = ...;
}
在循环内移动查询命令:

if (mysqli_query($mysqli, $sql)) {
for ($id = 1; $id <=$student_count_count; $id++)
{  
    $sql = ...
    if (mysqli_query($mysqli, $sql)) {
        ...
    } else {
        ...
    }
}
如果没有大括号,则while仅循环其后面的一行。若要循环多行,需要将这些行用大括号括起来:

while($row2 = mysqli_fetch_row($rcount_student_lists))
{
    $student_count_count = $row2['0'];
    for ($id = 1; $id <=$student_count_count; $id++)
    {
        ...
    }
}
while($row2=mysqli\u fetch\u row($rcount\u student\u list))
{
$student_count_count=$row2['0'];

对于($id=1;$id此循环响应并仅更新最后一条记录。啊,注意到另一个错误并更新了响应。注意,正确的缩进将使您受益匪浅。如果每一行都从第一列开始,则很难看到循环在何处断开。我发现此循环响应并仅更新最后一条记录。啊,注意到另一个错误并更新了响应。注意,正确的缩进将使您受益匪浅。如果每一行都从第一列开始,则很难看到循环在哪里断开。我找到了答案