简体   繁体   中英

insert mutiple php form array into mysql rows

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'");
    }
}

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'");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM