我正在使用包含三列的表格,即Roll_No,Name和Marks。我从数据库中获取Roll_no和Name。教师只需在表格中输入相应学生名单和名称的标记。我的编码如下所示,但只输入的最后一个值是为数据库中的所有学生设置的。它没有正确插入sid和roll,但是会产生标记问题。我的标记数据库结构是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);*/
?>
答案 0 :(得分: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");
}
?>