Programme Name: BCS IT (Hons.
Course Code: CSC 2514
Course Name: Web System and Technologies
Assignment / Lab Sheet / Project / Case Study No. 2
Date of Submission: 10th August, 2021
Submitted By: Submitted To:
Student Name: Rakshya Nepali Faculty Name: Prakash Chandra
IUKL ID: 041902900050 Department: LMS
Semester: Fourth
Intake: Sep, 2019
1.
a. With an example, explain the usage of htmlspecialchars() function.
➢ The htmlspecialchars() function converts special characters into HTML entities. It is
the in-built function of PHP, which converts all predefined characters to the HTML
entities. The predefined characters are:
& (ampersand) converted as &
" (double quote) converted as "
' (single quote) converted as '
< (less than) converted as <
> (greater than) converted as >
There is a string function htmlspecialchars_decode(), which is the reverse of the
htmlspecialchars() function. The main purpose of htmlspecialchars_decode() function
is to convert special HTML entities back to characters. htmlspecialchars() and
htmlspecialchars_decode() function are opposite to each other.
For example:
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes
?>
b. Write PHP code to show differences between local and global variables.
➢ PHP code to show the differences between local and global variables are given below:
Local variable:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
OUTPUT:
Global variable:
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
OUTPUT:
2. For the figure given below:
a. Write required HTML and PHP code for the following figure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Document</title>
</head>
<body>
<hr>
<div>
<h3>Enter the student details<h3>
<form action="insert.php" method="post">
<table>
<tr>
<td>Name</td>
<td> <input type="text" Name="name">
<td>
</tr>
<tr>
<td>Marks obtained</td>
<td> <input type="text"
Name="Subject1" value="Subject1">
<td>
<td> <input type="text"
Name="Subject2" value="Subject2">
<td>
<td> <input type="text"
Name="Subject3" value="Subject3">
<td>
</tr>
<tr>
<td> </td>
<td> <input type="submit"
name="submit"> </td>
</tr>
</table>
</form>
</div>
<hr>
</body>
</html>
b. For the fig .1 write required PHP code to collect form data and insert it into
database table, with following structure :
PHP code to create table:
CREATE TABLE `rakshya_database`.`details` ( `id` INT NOT NULL
AUTO_INCREMENT , `name` VARCHAR(100) NOT NULL , `subject1` DECIMAL(6,2)
NOT NULL , `subject2` DECIMAL(6,2) NOT NULL , `subject3` DECIMAL(6,2) NOT
NULL , PRIMARY KEY (`id`))
MySql connection code:
<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "rakshya_database";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect MySql Server:' .mysql_error());
}
?>
PHP code for inserting the data:
<?php
include_once 'database.php';
// $name=$Subject1=Subject2=Subject3="";
if(isset($_POST['submit']))
$name = $_POST['name'];
$Subject1= $_POST['Subject1'];
$Subject2= $_POST['Subject2'];
$Subject3 = $_POST['Subject3'];
$sql = "INSERT INTO details (name,Subject1,Subject2, Subject3)
VALUES ('$name','$Subject1','$Subject2','$Subject3')";
if (mysqli_query($conn, $sql)) {
echo "New record has been added successfully !";
} else {
echo "Error: " . $sql . ":-" . mysqli_error($conn);
mysqli_close($conn);
?>
Output:
c. Fetch the result from the table in fig. 2 and display the result as shown in figure
below: NOTE: use function to calculate average marks.
<html>
<body>
<?php
$conn=mysqli_connect("localhost","root","","rakshya_database");
$sql="SELECT *from details";
$result=mysqli_query($conn,$sql) or die("Query Unsuccessful");
if(mysqli_num_rows($result)>0)
{
?>
<form > <fieldset style="width: 50%;">
<h3>Student Report Card</h3>
<table border="1" cellpadding="13" cellspacing="0">
<tr>
<th>Name</th>
<th colspan="3">Subjects</th>
<th>Average Mark</th>
</tr>
<?php
$i=0;
while($row=mysqli_fetch_array($result))
{
$i=$i+1;
?>
<tr>
<td>
<?php echo $row["name"]; ?>
</td>
<td style="padding-left: 50px;"> <?php echo $row["Subject1"]; ?></td>
<td style="padding-left: 50px;"><?php echo $row["Subject2"]; ?></td>
<td style="padding-left: 50px;"><?php echo $row["Subject3"]; ?></td>
<td>
<?php
$a=$row["Subject1"];
$b=$row["Subject2"];
$c=$row["Subject3"];
$avg=($a+$b+$c)/3;
echo $avg;
?>
</td>
</tr>
<?php } ?>
</table>
<?php } ?>
</fieldset>
</form>
</body>
</html>
Output: