Reputation: 3
I am having form with a table with three columns namely Roll_No, Name and Marks. I am fetching the Roll_no and Name from the database. The teacher only has to enter the marks in the form against the respective student's roll no and name. I coded as shown below, but only the last value entered is getting set for all the students in the database. Its inserting the sid and roll no correctly but creating problem with marks. My database structure for marks is sid, roll_no, marks.
Here is my code: correct me where am going wrong please.
<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);*/
?>
Upvotes: 0
Views: 1824
Reputation: 13535
Your issue is you have the insert logic wrapped inside the loop which is displaying the rows of the table. Which was being executed completely wrong. I have adjust your code and here is a version that can work. This is not also 100 using best practices i've only adjust so that you can understand the change that has been made.
<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");
}
?>
Upvotes: 1