In: Computer Science
How can I make a PHP web app that will record student's names and grades?
Req.
student_list.php
<!DOCTYPE html>
<html>
<head>
<title>Student list</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Number Grade</h>
<th>Letter Grade</h>
</tr>
<?php
$conn = mysql_connect("localhost", "username",
"password", "StudentDB");
if ($conn -> connect_error)
{
die("connection failed: ". $conn->
connect_error);
}
$sql = "SELECT stname, stgrade from studentab";
$results = $conn -> query($sql);
if ($result-> num_rows >0)
{
while($row = $result->
fetch_assoc()))
{
echo
"<tr><td>". $row["stname"] ."</td><td>".
$row["stgrade"]. "</td>";
if($row[stgrade]
< 60)
{
echo
"<tr><td>". "F" ."</td>";
}
elseif($row[stgrade] >= 60 and $row[stgrade]<=69)
{
echo "<tr><td>". "D"
."</td>";
}
elseif($row[stgrade] >= 70 and $row[stgrade]<=79)
{
echo "<tr><td>". "C"
."</td>";
}
elseif($row[stgrade] >= 80 and $row[stgrade]<=89)
{
echo "<tr><td>". "B"
."</td>";
}
else
{
echo "<tr><td>". "A"
."</td>";
}
}
echo "</table>";
}
else
{
echo("0 Result");
}
$conn-> close();
?>
</table>
<form action="add_student.php" method="post">
<h2>Form to add student</h2>
<label>Student Name:</label>
<input class="input" name="name" type="text" value="">
<label>Grade:</label>
<input class="input" name="grade" type="text" value="">
<input class="submit" name="submit" type="submit"
value="Insert">
</form>
<form action="delete_student.php" method="post">
<h2>Form to delete student</h2>
<label>Student Name:</label>
<input class="input" name="name" type="text" value="">
<input class="submit" name="submit" type="submit"
value="Insert">
</form>
</body>
</html>
add_student.php
<?php
$conn = mysql_connect("localhost", "username", "password", "StudentDB");
if ($conn -> connect_error)
{
die("connection failed: ". $conn->
connect_error);
}
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$grade = $_POST['grade'];
if($name !=' ' || $grade !=' ')
{
$conn -> query("Insert into studenttab(stname, stgrade) values ('$name', '$grade')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
echo "<a href=". "student_list.php". ">Display List</a> "
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
} $conn-> close(); ?>
delete_student.php
<?php
$conn = mysql_connect("localhost", "username", "password", "StudentDB");
if ($conn -> connect_error)
{
die("connection
failed: ". $conn-> connect_error);
}
if(isset($_POST['submit']))
{
$name = $_POST['name'];
if($name !=' ')
{
$conn -> query("DELETE FROM studenttab where stname= '$name')");
echo "<br/><br/><span>Data Deleted successfully...!!</span>";
echo "<a href=". "student_list.php". ">Display List</a> "
}
else{
echo "<p>Deletion Failed <br/> Some Fields are Blank....!!</p>";
}
}
} $conn-> close(); ?>