0

我的表格包含三列,即 Roll_No、Name 和 Marks。我正在从数据库中获取 Roll_no 和 Name。老师只需在表格中输入相应学生的卷号和姓名即可。我编码如下所示,但只有最后输入的值是为数据库中的所有学生设置的。它没有正确插入 sid 和滚动,但会产生标记问题。我的标记数据库结构是 sid、roll_no、marks。

这是我的代码:请纠正我哪里出错了。

<form method="post" action="">
<table width="900" height="49" border="1" cellpadding="0" cellspacing="0" align="center" >
 <?php if(isset($_SESSION['response']))
      { 
echo "<tr><td colspan=\"7\">".$_SESSION['response']."</td></tr>";    unset($_SESSION['response']);} ?>

    <tr>
        <td align="center" width="20"> Roll No.</td>
        <td align="center" width="200"> Name</td>
        <td align="center" width="20"> Marks</td>
    </tr>
    <tr><input type="submit" name="next" value="submit" /></tr>
    <?php
    $sub = $_GET['sid'];

    $result = mysql_query("select * from student_record_by_faculty where sid='$sub'");
    $numrows = mysql_num_rows($result);
    if($numrows!=0)
{
        while($r=mysql_fetch_array($result))
        {
            $r2=mysql_fetch_array(mysql_query("select name from login where username='$r[reg_no]'"));


    ?>
    <tr>
    <td align="center"><?php echo $r['reg_no']; ?></td>
    <td align="center"><?php echo $r2['name']; ?></td>
    <td align="centre"><input type="number" name="marks" />
    <?php
    if(isset($_POST['next']))
    {
        $marks = $_POST['marks'];
        $q=mysql_query("INSERT INTO marks(sid, reg_no, first_sessional) VALUES('$sub','$r[reg_no]','".$_POST['marks']."')");
        echo $q;
        if($q)
        {       echo "marksheet updated you are being directed to marksheet page";
             sleep(2);
                header("Location:marksheet.php");
        }
    }
    ?>    
    </td></tr> 


  <?php                         
     }
}
    else
    {
   die('Error: ' . mysql_error());
}
//mysqli_close($con);*/
?> 

4

1 回答 1

1

您的问题是您将插入逻辑包装在显示表行的循环中。这是完全错误的。我已经调整了你的代码,这是一个可以工作的版本。这也不是 100 使用最佳实践,我只是调整了以便您可以了解所做的更改。

<form method="post" action="">
<table width="900" height="49" border="1" cellpadding="0" cellspacing="0" align="center" >
<?php 
    if(isset($_SESSION['response']))
    { 
        echo "<tr><td colspan=\"7\">".$_SESSION['response']."</td></tr>";    unset($_SESSION['response']);} ?>
<tr>
    <td align="center" width="20"> Roll No.</td>
    <td align="center" width="200"> Name</td>
    <td align="center" width="20"> Marks</td>
</tr>
<tr><td colspan='3'><input type="submit" name="next" value="submit" /></td></tr>
<?php
$sub = $_GET['sid'];

$result = mysql_query("select * from student_record_by_faculty where sid='" . mysql_real_escape_string($sub) . "'");
$numrows = mysql_num_rows($result);
if($numrows!=0)
{
    while(($r=mysql_fetch_array($result)) != FALSE)
    {
        $r2=mysql_fetch_array(mysql_query("select name from login where username='$r[reg_no]'"));
        ?>
<tr>
    <input type="hidden" name="reg_no[]" value="<?php echo $r['reg_no']; ?>" />
    <td align="center"><?php echo $r['reg_no']; ?></td>
    <td align="center"><?php echo $r2['name']; ?></td>
    <td align="center"><input type="number" name="marks[]" /></td>
</tr> 
<?php                         
    }
}    ?>
</table>
    <input type="hidden" name="subid" value="<?php echo $_GET['sid']; ?>" />
</form>

<?php 
//mysqli_close($con);*/


if(isset($_POST['next']))
{
    $marks = $_POST['marks'];
    $i=0;
    foreach ($marks as $mark) {
        $q=mysql_query("INSERT INTO marks(sid, reg_no, first_sessional) VALUES('" . $_GET['subid'] . "','" . $_POST['reg_no'][$i++] . "','" . $mark ."')");
    }
    header("Location:marksheet.php");

}


?> 
于 2013-06-17T05:02:38.427 回答