hi all
i want to add all students grades in one form
and then save each grade to the correct student
i make 2 differents php page first one display all student id that is already registered in my website
in this page instructor can add grade for each student id
then when click save
it will go to another php that take all student id as array
and must chick all id and put a correct mark with suitable id
this is the first php

<?php

include ('createDB.php');

mysql_query($sql,$con);

$ID=0;

$result = mysql_query("SELECT * FROM StudentsGrades");
 

echo "<table border='1'>";

// Execute query
while($row = mysql_fetch_array($result))
  {
// print table data 
  echo "<tr>";
  echo "<td>" . $row['student_ID'] . "</td>";
  echo "<td><form action='g1.php' method='post'><input type='text' name='grade' id='".$ID."'  /></td></tr>";
$ID = $ID + 1;
}
echo "<tr><td><input type='submit' value='save'  /></form></td></tr>";
echo "</table>";


?>

and the second

<?php

include ('createDB.php');


//select a DB
mysql_select_db("www_app", $con);

$select1= mysql_query("SELECT * FROM StudentsGrades");

$count = mysql_num_rows($select1);

while ($row=mysql_fetch_array($select1)) {


for($i = 0; $i <= $count; $i = $i + 1)
$result[] = ($row['student_ID']);

}
//**********************************************

$doc = new DomDocument;

$grades= $_POST['grade'];

 
for($j = 0; $j <= $count; $j = $j + 1)
$gradesArr[] = $doc->getElementById('j');



//===============================================

foreach($gradesArr as $keyGrade => $valueGrade)
{
foreach($result as $key => $valueID)
{
if($key ==$keyGrade)
{
$sql=("UPDATE StudentsGrades SET grade ='{$valueGrade}'
WHERE student_ID ='{$valueID}'"); 

echo "first ".$valueGrade;
echo " ".$valueID;

}//if 
echo "second  " .$valueGrade;
echo " ".$valueID;
}//seconf for
echo "therd  ".$valueGrade;
echo " ".$valueID;
}//first for 

mysql_query($sql,$con);	

mysql_close($con);
?>

please i need answer as soon as possible

Member Avatar for diafol

> please i need answer as soon as possible

Did you leave your assignment until the last minute?

You're producing loads of forms? This is BAD as you're incrementing the ID via loop. What happens if you delete a student? Your loop will be out of sync with the actual row id.

I'd start again if I were you. The 1st page forms need to be made into a single form as you mention. The question is what do you do with the data then? Some grades will not exist - so you need to use INSERT. If you change a grade, you'll need to use UPDATE.

This is a common problem, You can get around this issue with the REPLACE syntax - BUT the table needs to have a primary key.

Put the form opening tag and closing tags outside of the table to start with. Forget about your $ID variable, use the student id instead and ensure this is the primary key of the table or at the very least contains unique values only in your database (but by the name, a primary key sounds like what you need).

On your second PHP forget the select it's not necessary. Just use your incoming student_ids to reference the rows using update queries. Looping through the $_POST array should do it.

no it still 2 days but i must start the next phase

ok what i mean is i need instructor to insert all students grades at the same time in the first page
then in the second page save this grades in student grade table which is already contain othr info for student like email phone etc..
so i need to update not insert
but i didn't know how can i do it

please any answer

i need your help

Member Avatar for diafol
> so i need to update not insert

Well, from the looks of it, you're using a flat file db as opposed to a relational model. It may be valid for your example though.

Place all students with their grade input boxes into ONE form. Give each one a name with the id as an array key:

<input name="grade[<?php echo $row['id'];?>]" type="text" />

Your form handling page can then process the

$_POST['grade']

variable as an array:

foreach($_POST['grade'] as $key => $value){
  ... do an update based on DB id = $key and DB grade = $value ...
}

BTW you should check/sanitize your data before placing it into your DB.

thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanx
i will try it now

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.