PHPpracticalfile
PHPpracticalfile
MANAGEMENT SCIENCES
PRACTICAL FILE
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>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "studentDB";
$rollno = $_POST['rollno'];
$name = $_POST['name'];
$email = $_POST['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";
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";
mysqli_close($conn);
?>
Output:
4. Program to create, read, write, append and delete a file.
<?php
//creating a file
$file = fopen("data.txt", "w");
?>
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.
<!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="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>
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['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;
}
// 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;
}