0% found this document useful (0 votes)
3 views

PHPpracticalfile

The document outlines a practical file for a web application using PHP, detailing various tasks such as creating a student registration form, handling database operations, and implementing features like file management and exception handling. It includes code snippets for form submission, data retrieval, stored procedures, and server-side validation. Additionally, it demonstrates concepts like inheritance and nested tables in HTML.

Uploaded by

Saransh Patwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PHPpracticalfile

The document outlines a practical file for a web application using PHP, detailing various tasks such as creating a student registration form, handling database operations, and implementing features like file management and exception handling. It includes code snippets for form submission, data retrieval, stored procedures, and server-side validation. Additionally, it demonstrates concepts like inheritance and nested tables in HTML.

Uploaded by

Saransh Patwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

JAGANNATH INSTITUTE OF

MANAGEMENT SCIENCES

WEB APPLICATION USING PHP

PRACTICAL FILE

Submitted to: Submitted by:


-Ms. Poonam Malik - Divyanshi Shukla
- BCA ‘4th’ Sem
- 70/ITSD/2022
1. Use get post and request global variable to submit the data through form in
database table in php.

Creating table:

Creating Form:

<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="submit.php" method="post">
<center>
<h1>Student Registration Form</h1>
<label for="rollno">Rollno:</label>
<input type="number" name="rollno"><br><br>
<label for="name">Name:</label>
<input type="text" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" name="email"><br><br>
<input type="submit" value="Submit">
</center>
</form>
</body>
</html>

Creating php file:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "studentDB";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn === false)


{
die("Error occured. Connection failed" . mysqli_connect_error());
}

$rollno = $_POST['rollno'];
$name = $_POST['name'];
$email = $_POST['email'];

// Use $_REQUEST to support both GET and POST


// $rollno = $_REQUEST['rollno'];
// $name = $_REQUEST['name'];
// $email = $_REQUEST['email'];

$sql = "INSERT INTO student VALUES ('$rollno','$name','$email')";

if(mysqli_query($conn, $sql))
{
echo "Record inserted successfully!!";
}
else
{
echo "Error: " . mysqli_error($conn);
}

mysqli_close($conn);

?>
Output:
2. Fetch table data from database and show it in tabular form.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "studentDB";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn === false)


{
die("Error occured. Connection failed" . mysqli_connect_error());
}

$query = "SELECT * FROM `student`;";

$result = mysqli_query($conn, $query);

mysqli_close($conn);
?>

<html>
<head>
<style>
h2{
text-align: center;
}
table{
margin: 0 auto;
font-size: large;
border: 1px solid black;
}
td{
background-color: #fffff0;
border: 1px solid black;
}
th,td {
font-weight: bold;
border: 1px solid black;
padding: 10px;
text-align: center;
}
td {
font-weight: lighter;
}
</style>
</head>
<body>
<h2>Student details</h2>
<table>
<tr>
<th>Roll no</th>
<th>Name</th>
<th>Email</th>
</tr>
<?php
while($rows=mysqli_fetch_assoc($result))
{
?> <tr>
<td><?php echo $rows['Rollno'];?></td>
<td><?php echo $rows['Name'];?></td>
<td><?php echo $rows['Email'];?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>

Output:
3. Create stored procedure in php.

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "studentDB";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn === false)


{
die("Error occured. Connection failed" . mysqli_connect_error());
}

$query = mysqli_query($conn,"CREATE PROCEDURE display() SELECT * FROM student");

echo "Stored Procedure created.<br/>";

echo "*Calling Stored procedure to display the values from table*<br/><br/>";

$result = mysqli_query($conn,"CALL display()");

while ($rows = mysqli_fetch_assoc($result))


{
echo "Roll No: " . $rows["Rollno"]
. " | Name: " . $rows["Name"]
. " | Email: " . $rows["Email"]. "<br>";
}

mysqli_close($conn);

?>

Output:
4. Program to create, read, write, append and delete a file.

<?php
//creating a file
$file = fopen("data.txt", "w");

//writing in the file


$txt = "This is some text and information\n";
fwrite($file, $txt);

//closing the file


fclose($file);

//reading from the file


$rfile = fopen("data.txt", 'r');
$res = fread($rfile,filesize("data.txt"));
echo $res . "<br/>";
fclose($rfile);

//appending in the file


$afile = fopen("data.txt", 'a');
$new_txt = "These are some more facts\n";
fwrite($afile, $new_txt);
fclose($afile);

//reading from the file after appending


$rfile1 = fopen("data.txt", 'r');
clearstatcache(); // Clear cached file status information
$new_res = fread($rfile1,filesize("data.txt"));
echo $new_res;
fclose($rfile1);

//delete the file


$my_file = 'data.txt';
unlink($my_file);

?>

Output:
5. Create a nested table by using maximum table properties and make use of colspan
and rowspan and also insert a picture into the table cell.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Comprehensive Nested Table Example</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background-color: #fff;
}
table, th, td {
border: 1px solid #ccc;
}
th, td {
padding: 10px;
text-align: center;
}
th {
background-color: #e2e2e2;
}
.nested-table {
width: 100%;
border: 1px solid #666;
background-color: #f9f9f9;
}
.nested-table th, .nested-table td {
border: 1px solid #aaa;
}
.image-cell img {
width: 150px;
height: 100px;
object-fit: cover;
border-radius: 8px;
border: 1px solid #ddd;
}
h1 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
caption {
font-weight: bold;
margin-bottom: 10px;
color: #555;
}
</style>
</head>
<body>
<h1>BEVERAGES</h1>
<table>
<tbody>
<tr>
<td rowspan="3">
<table class="nested-table">
<caption>DRINKS</caption>
<tbody>
<tr>
<td>Fruit Juices</td>
<td>Milk Drinks</td>
</tr>
<tr>
<td class="image-cell">
<img src="juice.jpg" alt="Fruit juice Image">
</td>
<td class="image-cell">
<img src="shake1.jpg" alt="Milk drink Image">
</td>
</tr>
<tr>
<td class="image-cell">
<img src="juice1.jpg" alt="Fruit juice Image">
</td>
<td class="image-cell">
<img src="shake2.jpg" alt="Milk drink Image">
</td>
</tr>
</tbody>
</table>
</td>
<td colspan="2">MOCKTAILS</td>
</tr>
<tr>
<td class="image-cell">
<img src="mocktail.jpg" alt="Mocktail Image">
</td>
<td class="image-cell">
<img src="mocktail1.png" alt="Mocktail Image">
</td>
</tr>
<tr>
<td class="image-cell">
<img src="mock.jpg" alt="Mocktail Image">
</td>
<td class="image-cell">
<img src="mock1.jpg" alt="Mocktail Image">
</td>
</tr>
</tbody>
</table>
</body>
</html>

Output:
6. Create a Student Registration Form with server side validation.

Student Registration Form:

<!DOCTYPE html>
<html>
<head>
<title>Student Registration Form</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
margin: 0;
padding: 0;
}
.form-style {
background-color: #fff;
padding: 20px;
margin: 50px auto;
width: 400px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
legend {
font-size: 24px;
margin-bottom: 20px;
color: #333;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #666;
}
input[type="text"],
input[type="email"],
input[type="number"],
textarea,
select {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
background-color: #f9f9f9;
color: #333;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.error {
color: red;
margin-top: 5px;
}
</style>
</head>
<body>
<div class="form-style">
<form name="InfoForm" action="save.php" method="POST">
<fieldset>
<legend>Student Registration Form</legend>
<label for="studentname">Name of the student:</label>
<input type="text" id="studentname" name="studentname" required>

<label for="rollno">Roll no.:</label>


<input type="text" id="rollno" name="rollno" required>

<label for="email">Email:</label>
<input type="email" id="email" name="email" required>

<label for="age">Age:</label>
<input type="number" id="age" name="age" required>

<label>Gender:</label><br>
<input type="radio" name="gender" value="male" required> Male
<input type="radio" name="gender" value="female"> Female
<input type="radio" name="gender" value="others"> Others

<br>
<br>
<label for="Course">Course:</label>
<br>
<select name="Course" id="Course" required>
<option value="computer science">Computer Science</option>
<option value="maths">Maths</option>
<option value="chemistry">Chemistry</option>
<option value="physics">Physics</option>
<option value="zoology">Zoology</option>
</select>

<label for="address">Enter Address:</label><br>


<textarea name="address" id="address" rows="5" cols="30" required></textarea>

<input type="submit" name="submit" value="Submit">


</fieldset>
</form>
</div>
</body>
</html>

Output :

Save.php File:

<?php
$errors = [];

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST['studentname'])) {
$errors[] = "Name is required.";
}
else
{
if (!preg_match("/^[a-zA-Z ]*$/",$studentname))
{
echo "Only alphabets and white space are allowed.";
}
}

if (empty($_POST['rollno'])) {
$errors[] = "Roll number is required.";
}

if (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {


$errors[] = "Valid email is required.";
}

if (empty($_POST['age']) || $_POST['age'] < 18) {


$errors[] = "Age must be at least 18 years old.";
}

if (empty($_POST['gender'])) {
$errors[] = "Gender is required.";
}

if (empty($_POST['Course'])) {
$errors[] = "Course is required.";
}

if (empty($_POST['address'])) {
$errors[] = "Address is required.";
}

if (empty($errors)) {
echo "Registration successful!";
exit();
}
}

?>
OUTPUT:
7. Create a exceptional handling program in php.

<?php
function divide($dividend, $divisor)
{
if ($divisor == 0)
{
throw new Exception("Division by zero.");
}
return $dividend / $divisor;
}
try
{
$result = divide(10, 0);
echo "Result: " . $result;
}
catch (Exception $e)
{
echo "Caught exception: " . $e->getMessage();
}
finally
{
echo "\n End of script.";
}
?>

Output :
8. Create a program for constructor and Inheritance.

<?php
// Base Class
class Person
{
protected $name;
protected $age;

// Constructor
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}

public function display() {


echo "Name: " . $this->name . "<br>";
echo "Age: " . $this->age . "<br>";
}
}

// Derived Class
class Student extends Person {
private $studentId;

// Constructor
public function __construct($name, $age, $studentId) {
// Call the parent constructor
parent::__construct($name, $age);
$this->studentId = $studentId;
}

public function display() {


parent::display();
echo "Student ID: " . $this->studentId . "<br>";
}
}

// Create an instance of the Student class


$student = new Student("Jatin", 20, "S12345");
$student1 = new Student("Harshita", 20, "S22345");
$student2 = new Student("Ansh", 20, "S20345");
$student->display();
$student1->display();
$student2->display();
?>
Output :

You might also like