Hello. :) So I am creating a grading system where users can compute student's grades on selected ID Number. The problem is after clicking the COMPUTE button, an "Error!" message display. So far I can save the grades in database but I can't compute it. I'm sorry I'm new to PHP. Here is my code so far:

<?php
 function renderForm($ID, $FirstName, $LastName, $Attendance, $Quiz, $MidtermExam, $FinalExam, $FinalGrade, $Remarks, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>COMPUTE GRADE</title>
 </head>
 <body>
 <?php 
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
 
 <form action="grade1.php" method="POST">

<td>&nbsp;</td>
<br /><td>&nbsp;</td><label>ID Number: </label><input type="text" name="ID" value="<?php echo $ID; ?>" READONLY /><br />
<br /><td>&nbsp;</td><label>First Name: </label><input type="text" name="FirstName" value="<?php echo $FirstName; ?>" READONLY /><br />
<br /><td>&nbsp;</td><label>Last Name: </label><input type="text" name="LastName" value="<?php echo $LastName; ?>" READONLY /><br /><br />

<br /><td>&nbsp;</td><label>Attendance: </label><input type="text" name="Attendance" value="<?php echo $Attendance; ?>"/><br /><br />
<br /><td>&nbsp;</td><label>Quiz: </label><input type="text" name="Quiz" value="<?php echo $Quiz; ?>"/><br /><br />
<br /><td>&nbsp;</td><label>Midterm Exam: </label><input type="text" name="MidtermExam" value="<?php echo $MidtermExam; ?>"/><br /><br />
<br /><td>&nbsp;</td><label>Final Exam: </label><input type="text" name="FinalExam" value="<?php echo $FinalExam; ?>"/><br /><br />
<br /><td>&nbsp;</td><label>Final Grade: </label><input type="text" name="FinalGrade" value="<?php echo $FinalGrade; ?>" READONLY /><br /><br />
<br /><td>&nbsp;</td><label>Remarks: </label><input type="text" name="Remarks" value="<?php echo $Remarks; ?>" READONLY /><br /><br />


<br /><td>&nbsp;</td><input type="submit" value="COMPUTE" name="compute"/><br /><br />
<br /><td>&nbsp;</td><input type="submit" value="SAVE" name="save"/><br /><br />
</form>
</body>
</html>

 <?php
 }

 include('collegeinfo_connect.php');

 if (isset($_POST['save']))
 { 

 if (is_numeric($_POST['ID']))
 {

 $ID = mysql_real_escape_string(htmlspecialchars($_POST['ID']));
 $FirstName = mysql_real_escape_string(htmlspecialchars($_POST['FirstName']));
 $LastName = mysql_real_escape_string(htmlspecialchars($_POST['LastName']));
 $Attendance = mysql_real_escape_string(htmlspecialchars($_POST['Attendance']));
 $Quiz = mysql_real_escape_string(htmlspecialchars($_POST['Quiz']));
 $MidtermExam = mysql_real_escape_string(htmlspecialchars($_POST['MidtermExam']));
 $FinalExam = mysql_real_escape_string(htmlspecialchars($_POST['FinalExam']));
 $FinalGrade = mysql_real_escape_string(htmlspecialchars($_POST['FinalGrade']));
 $Remarks = mysql_real_escape_string(htmlspecialchars($_POST['Remarks']));

 if ($ID == '' || $FirstName == '' || $LastName == '' || $Attendance == '' || $Quiz == '' || $MidtermExam == '' || $FinalExam == '' || $FinalGrade == '' || $Remarks == '')
 {

 $error = 'ERROR: Please fill in all required fields!';

 renderForm($ID, $FirstName, $LastName, $Attendance, $Quiz, $MidtermExam, $FinalExam, $FinalGrade, $Remarks, $error);
 }
 else
 {

 mysql_query("UPDATE collegeinfo_tbl SET FirstName='$FirstName', LastName='$LastName', Attendance='$Attendance', Quiz='$Quiz', MidtermExam='$MidtermExam', FinalExam='$FinalExam', FinalGrade='$FinalGrade', Remarks='$Remarks' WHERE ID='$ID'")
 or die(mysql_error()); 

 header("Location: results.php"); 
 }
 }
 else
 {

 echo 'Error!';
 }
 }
 else
 {

 if (isset($_GET['ID']) && is_numeric($_GET['ID']) && $_GET['ID'] > 0)
 {

 $ID = $_GET['ID'];
 $result = mysql_query("SELECT * FROM collegeinfo_tbl WHERE ID=$ID")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);

 if($row)
 {
 $ID = $row['ID'];
 $FirstName = $row['FirstName'];
 $LastName = $row['LastName'];
 $Attendance = $row['Attendance'];
 $Quiz = $row['Quiz'];
 $MidtermExam = $row['MidtermExam'];
 $FinalExam = $row['FinalExam'];
 $FinalGrade = $row['FinalGrade'];
 $Remarks = $row['Remarks'];

 

 renderForm($ID, $FirstName, $LastName, $Attendance, $Quiz, $MidtermExam, $FinalExam, $FinalGrade, $Remarks, '');
 }
 else
 {
 echo "No results!";
 }
 }
 else
 {
 echo 'Error!'; <-- An error displays after clicking compute button.
 }
 }
 
  if (isset($_POST['compute']))
       {
	   
	   $Attendance = $_POST['Attendance'];
	   $Quiz = $_POST['Quiz'];
       $MidtermExam = $_POST['MidtermExam']; 
	   $FinalExam = $_POST['FinalExam'];
	   
	    $solve1 = ($_POST['Attendance'] * 0.10);
		$solve2 = ($_POST['Quiz'] * 0.30);
		$solve3 = ($_POST['MidtermExam'] * 0.25);
		$solve4 = ($_POST['FinalExam'] * 0.35);
		
		$add = ($solve1 + $solve2 + $solve3 + $solve4);
		
		$FinalGrade = round($add);
		
		if ($add >= 75) 
	  	 {
          $Remarks = "PASSED";
        }
       else {
           $Remarks = "FAILED";
       }	
	}	
?>

Or do you have any simple code for grading system where it can save in database from selected ID number? I hope it made sense. Any help would be appreciated.

Dani AI

Generated

Short diagnosis: the page shows "Error!" because the script falls into the GET-check branch before your compute code runs. Clicking COMPUTE sends a POST (no $_GET['ID']), so the code that expects a GET id hits the else { echo 'Error!'; } and prints that message β€” your isset($_POST['compute']) block is later in the file, so it runs too late to avoid the error. Fix by handling POST actions first (compute/save), then run the GET renderer.

A minimal, safe structure to follow (handle POST then GET):

// handle POST actions before any GET-only checks
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['compute'])) {
        $attendance = isset($_POST['Attendance']) ? floatval($_POST['Attendance']) : 0;
        $quiz       = isset($_POST['Quiz']) ? floatval($_POST['Quiz']) : 0;
        $mid        = isset($_POST['MidtermExam']) ? floatval($_POST['MidtermExam']) : 0;
        $final      = isset($_POST['FinalExam']) ? floatval($_POST['FinalExam']) : 0;

        $finalGrade = round($attendance * 0.10 + $quiz * 0.30 + $mid * 0.25 + $final * 0.35);
        $remarks    = ($finalGrade >= 75) ? 'PASSED' : 'FAILED';

        // re-render the form with computed values (do not redirect)
        renderForm($_POST['ID'], $_POST['FirstName'], $_POST['LastName'],
                   $attendance, $quiz, $mid, $final, $finalGrade, $remarks, '');
        exit;
    }

    if (isset($_POST['save'])) {
        // validate and persist (use prepared statements / mysqli or PDO)
    }
}

// then handle GET to populate form for first load

Troubleshooting tips: turn on errors (error_reporting(E_ALL); ini_set('display_errors',1);) and inspect var_dump($_POST) to confirm what is sent. Remember readonly fields submit but disabled ones do not. Validate with is_numeric/floatval before arithmetic and clamp ranges if needed.

Security & workflow: compute on the server (client-side math can be spoofed) as warned, and persist the computed final grade in your table as suggested so students see only the saved, authoritative value. Also migrate away from deprecated mysql_* to mysqli or PDO with prepared statements before deploying.

Recommended Answers

All 2 Replies

Hi,

Is this for the faculty and student views? If for both, the student can easily spoof your form, and they can have 100% grades on all of the terms prelim, midterm and finals..


Just my humble advice :). Show the grades to student as text string and NOT on the form system. Provide them with the calculation link using jquery to calculate the grades. The jquery will then print the grades on the targeted div.

Also when calculating isolate each variable and constant with parenthesis.. eg. ((value1 * value2) + (value3 * value4))..

Calculation of grade must be one time process and it must be done by faculty or administrator, in that process it should store calculated grades in some table.

Then that calculated grades can be shown to students any other who knows roll number.

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.