Hi everyone! I am sorry I'm new to PHP and MySQL. So, I am creating a form for user to update their details and compute student's grade but the updating part doesn't work. Here is 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="grade.php?ID=' . $row['ID'] .'">COMPUTE</a></td>';
				echo "</tr>"; 
        } 

       
        echo "</table>";
?>

</body>
</html>

GRADE.PHP

<?php

  error_reporting(0);
?>
<?php 
 	         		
  if (isset($_REQUEST['solve']))
       {
	   
	   $Attendance = $_REQUEST['Attendance'];
	   $Quiz = $_REQUEST['Quiz'];
       $MidtermExam = $_REQUEST['MidtermExam']; 
	   $FinalExam = $_REQUEST['FinalExam'];
	   
	    $solve1 = ($_REQUEST['Attendance'] * 0.10);
		$solve2 = ($_REQUEST['Quiz'] * 0.30);
		$solve3 = ($_REQUEST['MidtermExam'] * 0.25);
		$solve4 = ($_REQUEST['FinalExam'] * 0.35);
		
		$add = ($solve1 + $solve2 + $solve3 + $solve4);
		
		$FinalGrade = round($add);
		
		if ($add >= 75) 
	  	 {
          $Remarks = "PASSED";
        }
       else {
           $Remarks = "FAILED";
       }	
	}	
	   
	if (isset($_REQUEST['save']))
       {
	  
$ID = $_REQUEST['ID'];
$Attendance = $_REQUEST['Attendance'];
$Quiz = $_REQUEST['Quiz'];
$MidterExam = $_REQUEST['MidtermExam'];
$FinalExam = $_REQUEST['FinalExam'];
$FinalGrade = $_REQUEST['FinalGrade'];
$Remarks = strtoupper($_REQUEST['Remarks']);


include('collegeinfo_connect.php');
	  

	$allowedFields = array(
	
	'Attendance',
	'Quiz',
	'MidtermExam',
	'FinalExam',
	'FinalGrade',
	'Remarks'
	);

$requiredFields = array(
	
	'Attendance',
	'Quiz',
	'MidtermExam',
	'FinalExam'
);

$errors = array();
foreach($_POST AS $key => $value)
{

	if(in_array($key, $allowedFields))
	{
		$$key = $value;
		

		if(in_array($key, $requiredFields) && $value == '')	
		{
			$errors[] = "The field $key is required.";
		}
	}	
}


if(count($errors) > 0)
{
	$errorString = '<p>There was an error processing the form.</p>';
	$errorString .= '<ul>';
	foreach($errors as $error)
	{
		$errorString .= "<li>$error</li>";
	}
	$errorString .= '</ul>';

	echo  $errorString;	
  }
  else {
  $sql = "UPDATE collegeinfo_tbl SET Attendance='$Attendance',Quiz='$Quiz',MidtermExam='$MidtermExam',FinalExam='$FinalExam',FinalGrade='$FinalGrade',Remarks='$Remarks' WHERE ID='$ID'";

  
			if(!$b=mysql_query($sql)){
				echo mysql_error();
			}
			else {
				    echo "<center>";
					echo "<h4> Record is Successfully added in the Database.</h4>";
					echo "</center>";
				    }
    }
  }
?>
	  
<html>
<head>
 <STYLE type="text/css">
     
      h2 { 
        text-align:center;
		color: white;                /* text color is white */ 
        background: green;            /* Content, padding will be blue */
        margin: 12px 12px 12px 12px;
        padding: 12px 0px 12px 12px; /* Note 0px padding right */
        list-style: none             /* no glyphs before a list item */
                                     /* No borders set */
      }
     
    </STYLE>
</head>
<body>
<h2> <marquee direction=left behavior=alternate>
COLLEGE STUDENT GRADING SYSTEM </marquee> </h2>
<br>
<FORM NAME="form1" METHOD="POST" ACTION="">

 <label>ID: </label><input type="text" name="ID" value="<?php echo $ID; ?>"/>
<TABLE BORDER="0">
  <tr> </tr> <tr> </tr>
 <TR>
    <TD>Attendance:</TD>
    <TD><INPUT TYPE="TEXT" NAME="Attendance" SIZE="1"  MAXLENGTH=3
	   value="<?php echo $Attendance; ?>">
	   
	</TD>

    <TD>Quiz: </TD>
    <TD><INPUT TYPE="TEXT" NAME="Quiz" SIZE="1" MAXLENGTH=3
	    value="<?php echo $Quiz; ?>">
	</TD>

    <TD>Midterm Exam:</TD>
    <TD><INPUT TYPE="TEXT" NAME="MidtermExam" SIZE="1" MAXLENGTH=3
	   value="<?php echo $MidtermExam; ?>">
	</TD>
 
   <TD>Final Exam:</TD>
    <TD><INPUT TYPE="TEXT" NAME="FinalExam" SIZE="1" MAXLENGTH=3
	    value="<?php echo $FinalExam; ?>" >
	</TD>
	
	<TD>Final Grade:</TD>
    <TD><INPUT TYPE="TEXT" NAME="FinalGrade" SIZE="1" MAXLENGTH=3
	    value="<?php echo $FinalGrade; ?>" READONLY>
	</TD>
    
	 <TD>Remarks:</TD>
    <TD>
      <INPUT TYPE="TEXT" NAME="Remarks" SIZE="10" 
 	  value="<?php echo $Remarks; ?>" READONLY>
	  </TD>
     </TR>
    </TABLE>

  
<P><input type="submit" name="solve" value="Compute"> 
<input type="submit" name="save" value=" Save  ">
</P>
</FORM>
</body>
</html>

TABLE:

-- phpMyAdmin SQL Dump
-- version 3.4.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 18, 2012 at 12:33 AM
-- Server version: 5.5.20
-- PHP Version: 5.3.9

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `collegeinfo_db`
--

-- --------------------------------------------------------

--
-- Table structure for table `collegeinfo_tbl`
--

CREATE TABLE IF NOT EXISTS `collegeinfo_tbl` (
  `ID` int(4) NOT NULL,
  `FirstName` varchar(50) NOT NULL,
  `LastName` varchar(50) NOT NULL,
  `Gender` varchar(10) NOT NULL,
  `Year` int(2) NOT NULL,
  `Course` varchar(50) NOT NULL,
  `Attendance` int(4) NOT NULL,
  `Quiz` int(4) NOT NULL,
  `MidtermExam` int(4) NOT NULL,
  `FinalExam` int(4) NOT NULL,
  `FinalGrade` int(4) NOT NULL,
  `Remarks` varchar(50) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `collegeinfo_tbl`
--

INSERT INTO `collegeinfo_tbl` (`ID`, `FirstName`, `LastName`, `Gender`, `Year`, `Course`, `Attendance`, `Quiz`, `MidtermExam`, `FinalExam`, `FinalGrade`, `Remarks`) VALUES
(101, 'John', 'Smith', 'Male', 3, 'BSIT', 50, 30, 65, 70, 87, PASSED);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

What is the error message (if any) that you get? Have you checked if the update query gets constructed as expected? Place the following code on line 97 in grade.php:

die($sql);

. It will stop the script at the point where the query is constructed and echo the query. You can have a look at it whether it is syntactically correct and you can paste it in phpmyadmin or mysql client to test it.

And to add an advice regarding the html code: the html code is OK but it is worth making it XHTML compliant (not a must but a good idea), unless you have a good reason to keep it as it is now. XHTML sode would be more future trouble proof as XHTML is already widely adopted and supported. This amongst other means that all the HTML tags should be in lower case and all tags should be terminated (i.e <br> should be <br />). As I said this is not a requirement just a recommendation.

Hi,

Look for line 72 grade.php

$$key = $value;

it should be

$key = $value;

Try using some PHP IDE like netbeans or eclipse... others settled in for advance php editor like personal php designer 2007. The good thing about them, they are all for FREE.. Load your entire script as new project to either netbeans or eclipse.. IDEs will at least look for errors and warnings found on the script. This can truly help in minimizing errors while writing codes.

Oh yeah! I figured it out! thank you so much guys. :)

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.