Hi, good morning,
Dynamically I pass student id, subject id, subject marks.
for example, assume that there are 50 students in a class, subjects are 5. so that 5 fields are there to enter 5 subjects mark. and the total will calculate automatically.

while storing in database , I have a problem that , I have done it using two for loops by counting student id in first loop , in second loop subject id, so that correctly I got 250 records, but my problem is, it stores only first student marks to all students, how to solve that.
Here is my code?

$std_id = $_REQUEST['std_id'];
$sub_id = $_REQUEST['sub_id'];
$sub_mark=$_REQUEST['sub_mark'];
$total=$_REQUEST['total'];

for($i=0;$i<count($std_id);$i++)
{
for($j=0;$j<count($sub_id);$j++)
{
mysql_query("INSERT INTO mark_tbl(std_id,sub_id,mark)VALUES('$std_id[$i]','$sub_id[$j]','$sub_mark[$j]')");
}
}

You don't want to be using submark[$j] as that just repeats the first values (0 through to whatever size j is).
You want to have another counter that starts at zero and increments at the end of each inner loop. Or structure your marks array to be two dimensional and then refer to submark[$i][$j]

Hi hericles,
Can you tell me where I have to change in my code.

    $std_id = $_REQUEST['std_id'];
    $sub_id = $_REQUEST['sub_id'];
    $sub_mark=$_REQUEST['sub_mark'];
    $total=$_REQUEST['total'];
    for($i=0;$i<count($std_id);$i++)
    {
    for($j=0;$j<count($sub_id);$j++)
    {
    mysql_query("INSERT INTO mark_tbl(std_id,sub_id,mark)VALUES('$std_id[$i]','$sub_id[$j]','$sub_mark[$j]')");
    }
    }

You haven't mentioned what form sub_marks takes so I'm going to assume it is a 1d array of marks, the first 5 elements belonging to student id 1, the next 5 to student ID 2, etc.
So, adding a counter and using that as the the sub_marks index will work

$std_id = $_REQUEST['std_id'];
$sub_id = $_REQUEST['sub_id'];
$sub_mark=$_REQUEST['sub_mark'];
$total=$_REQUEST['total'];
$counter = 0;
for($i=0;$i<count($std_id);$i++)
{
   for($j=0;$j<count($sub_id);$j++)
   {
     mysql_query("INSERT INTO mark_tbl(std_id,sub_id,mark)VALUES('$std_id[$i]','$sub_id[$j]','$sub_mark[$counter]')");
     $counter++;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.