dousi4148 2015-03-03 14:34
浏览 49
已采纳

将多个php表单数组插入mysql行

i am fetching data to create a form that would later be updated via php into their individual mysql rows. Here is what i have tried so far and it isn't working.

$subj='1';
    while($row=mysql_fetch_assoc($sql))
    {
    $go=mysql_query("select 1ca from result where subject_id='$subj' and student_id='$row[id]'"); $ca=mysql_fetch_assoc($go);
    ?>
    <tr><td><input type="hidden" name="std_id[]" value="<?php echo $row['id']; ?> "/><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></td>
    <td><input type="text" name="ca1[]" value="<?php echo $ca['1ca']; ?>"/></td></tr>
    <?php } ?>

and here is how I intend to process it so that each Students CA1 is updated accordingly

if(isset($_POST['submit']))
{

    foreach($_POST['std_id'] as $student)
    {
         foreach ($_POST['ca1'] as $ca1)
         {

         }
         mysql_query("update result set ca1='$ca1' where student_id='$student'");
    }
}
  • 写回答

1条回答 默认 最新

  • dounang1974 2015-03-03 15:55
    关注

    I don't fully understand the question but is this what you are wanting? Are the result arrays the same size and keys match up between them?

    if(isset($_POST['submit']))
    {
    
        foreach($_POST['std_id'] as $key => $student)
        {
             mysql_query("update result set ca1='{$_POST['ca1'][$key]}' where student_id='$student'");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?