Jump to content

PHP and MySQL: Can't COMPUTE GRADES


aianne

Recommended Posts

Hi everyone! My first post here in PHPFREAKS.  :D And I've got a problem about PHP. I'm sorry I'm new to PHP so, I'm creating a grading system program where users can compute student's grade from selected ID number. So far I can save the grades in database but I can't compute the grades, an "Error!" message displays after clicking the COMPUTE button. Here is my code:

 

COMPUTE.PHP

<!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

include('collegeinfo_connect.php');

$result = mysql_query("SELECT * FROM collegeinfo_tbl") 
or die(mysql_error());  

echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID:</th> <th>First Name:</th> <th>Last Name:</th> <th>Gender:</th> <th>Year:</th> <th>Course:</th> </tr>";

       
        while($row = mysql_fetch_array( $result )) {
                
                
                echo "<tr>";
                echo '<td>' . $row['ID'] . '</td>';
                echo '<td>' . $row['FirstName'] . '</td>';
                echo '<td>' . $row['LastName'] . '</td>';
			echo '<td>' . $row['Gender'] . '</td>';
			echo '<td>' . $row['Year'] . '</td>';
			echo '<td>' . $row['Course'] . '</td>';
			echo '<td><a href="grade1.php?ID=' . $row['ID'] .'">COMPUTE</a></td>';
			echo "</tr>"; 
        } 

       
        echo "</table>";
?>

</body>
</html> 

 

 

 

GRADE1.PHP

<?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> </td>
<br /><td> </td><label>ID Number: </label><input type="text" name="ID" value="<?php echo $ID; ?>" READONLY /><br />
<br /><td> </td><label>First Name: </label><input type="text" name="FirstName" value="<?php echo $FirstName; ?>" READONLY /><br />
<br /><td> </td><label>Last Name: </label><input type="text" name="LastName" value="<?php echo $LastName; ?>" READONLY /><br /><br />

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


<br /><td> </td><input type="submit" value="COMPUTE" name="compute"/><br /><br />
<br /><td> </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 [color=red]'Error!';[/color] [color=green] <---- an error displays after clicking compute button.[/color]
}
}

  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 you can give me a simple code that can compute grades from Selected student or any link. I hope it made sense. Any help would be appreciated.  :shy:

Link to comment
Share on other sites

Firstly, wrapping entire HTML pages inside functions isn't recommended. It's not wrong because it would theoretically work but I would strongly recommended/advise/insist on you not using that method for displaying pages for countless reasons; one being its probably one of the worst practices.

 

With regards to your error, would you be able to tell us what the error your receiving is? 

 

And welcome to the forums, hopefully we'll be able to help you out ;)

Link to comment
Share on other sites

Thank you for your reply and I'm so sorry about that. I'm fairly new to this. Anyway, An "Error!" displays after clicking the COMPUTE button in GRADE1.PHP form. I highlighted below the Error that displays after clicking the button.

 

 

<?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> </td>

<br /><td> </td><label>ID Number: </label><input type="text" name="ID" value="<?php echo $ID; ?>" READONLY /><br />

<br /><td> </td><label>First Name: </label><input type="text" name="FirstName" value="<?php echo $FirstName; ?>" READONLY /><br />

<br /><td> </td><label>Last Name: </label><input type="text" name="LastName" value="<?php echo $LastName; ?>" READONLY /><br /><br />

 

<br /><td> </td><label>Attendance: </label><input type="text" name="Attendance" value="<?php echo $Attendance; ?>"/><br /><br />

<br /><td> </td><label>Quiz: </label><input type="text" name="Quiz" value="<?php echo $Quiz; ?>"/><br /><br />

<br /><td> </td><label>Midterm Exam: </label><input type="text" name="MidtermExam" value="<?php echo $MidtermExam; ?>"/><br /><br />

<br /><td> </td><label>Final Exam: </label><input type="text" name="FinalExam" value="<?php echo $FinalExam; ?>"/><br /><br />

<br /><td> </td><label>Final Grade: </label><input type="text" name="FinalGrade" value="<?php echo $FinalGrade; ?>" READONLY /><br /><br />

<br /><td> </td><label>Remarks: </label><input type="text" name="Remarks" value="<?php echo $Remarks; ?>" READONLY /><br /><br />

 

 

<br /><td> </td><input type="submit" value="COMPUTE" name="compute"/><br /><br />

<br /><td> </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!'; <--- This 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";

      }

}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.