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

ET22BTEC010 - Diya Patel - Practical 11 - PHP Database

Uploaded by

0611diyapatel
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)
23 views

ET22BTEC010 - Diya Patel - Practical 11 - PHP Database

Uploaded by

0611diyapatel
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/ 9

Subject Code:-BTCO15504

Subject Name:- Web Technology Concepts


Name :- Diya Patel
Enrollment Number:- ET22BTEC010

Practical 11: PHP Database


1) Understand the PHP interface, Study PHPMyAdmin and Perform following:
Write a PHP script to create a database StudentDB.
Write a PHP script to list all the databases available in mysql.
Write a PHP script to list all the tables available in a particular database.
Write a PHP script to create a table student(Enroll_no,name,age,marks) in the database StudentDB.
Write a PHP script to insert a row into the table student. The values to be inserted are taken from a
HTML page.
Write a PHP script to list all the records in the student table in tabular format.

Write a PHP script to delete all rows from student table whose Enroll_no numbers are between 1
and 3.
Write a PHP script to display all rows from student table whose marks are >=35.
Write a PHP script to update row from the table student whose age is 30.
Program:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "StudentDB";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $dbname";
if ($conn->query($sql) === TRUE) {
SCET/CO/2024-25/ODD/BTech/Sem-V Page No.190

Subject Code:BTCO15504 Enrollment No: ET22BTEC012


Subject Name: WTC Name:RAJNIDHI GUPTA

Practical No:11

echo "Database created successfully<br>";


} else {
echo "Error creating database: " . $conn->error . "<br>";
}

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 122


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

// Select the database


$conn->select_db($dbname);
// Create table
$sql = "CREATE TABLE IF NOT EXISTS student (
Enroll_no INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
age INT(3) NOT NULL,
marks INT(3) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully<br>";
} else {
echo "Error creating table: " . $conn->error . "<br>";
}
// Insert a row from HTML form
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['insert'])) {
$name = htmlspecialchars($_POST['name']);
$age = intval($_POST['age']);
$marks = intval($_POST['marks']);
$sql = "INSERT INTO student (name, age, marks) VALUES ('$name', $age, $marks)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully<br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// List all databases
$sql = "SHOW DATABASES";
$result = $conn->query($sql);
echo "Databases available:<br>";
while ($row = $result->fetch_assoc()) {
echo $row['Database'] . "<br>";
}

// List all tables in StudentDB


$sql = "SHOW TABLES FROM $dbname";
$result = $conn->query($sql);
echo "Tables in database '$dbname':<br>";
while ($row = $result->fetch_assoc()) {
echo $row['Tables_in_'.$dbname] . "<br>";
}

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 123


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

// Display all records in student table in tabular format


$sql = "SELECT * FROM student";
$result = $conn->query($sql);
echo "<table border='1'><tr><th>Enroll_no</th><th>Name</th><th>Age</th><th>Marks</th></tr>";
while ($row = $result->fetch_assoc()) {
echo
"<tr><td>{$row['Enroll_no']}</td><td>{$row['name']}</td><td>{$row['age']}</td><td>{$row['marks']}</td
></tr>";
}
echo "</table>";
// Delete rows with Enroll_no between 1 and 3
$sql = "DELETE FROM student WHERE Enroll_no BETWEEN 1 AND 3";
if ($conn->query($sql) === TRUE) {
echo "Rows deleted successfully<br>";
} else {
echo "Error deleting rows: " . $conn->error . "<br>";
}
// Display rows with marks >= 35
$sql = "SELECT * FROM student WHERE marks >= 35";
$result = $conn->query($sql);
echo "Students with marks >= 35:<br>";
echo "<table border='1'><tr><th>Enroll_no</th><th>Name</th><th>Age</th><th>Marks</th></tr>";
while ($row = $result->fetch_assoc()) {
echo
"<tr><td>{$row['Enroll_no']}</td><td>{$row['name']}</td><td>{$row['age']}</td><td>{$row['marks']}</td
></tr>";
}
echo "</table>";
// Update rows with age = 30
$sql = "UPDATE student SET age = 29 WHERE age = 30";
if ($conn->query($sql) === TRUE) {
echo "Rows updated successfully<br>";
} else {
echo "Error updating rows: " . $conn->error . "<br>";
}
// Drop table student
$sql = "DROP TABLE IF EXISTS student";
if ($conn->query($sql) === TRUE) {
echo "Table dropped successfully<br>";
} else {
echo "Error dropping table: " . $conn->error . "<br>";

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 124


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

}
// Drop database StudentDB
$sql = "DROP DATABASE IF EXISTS $dbname";
if ($conn->query($sql) === TRUE) {
echo "Database dropped successfully<br>";
} else {
echo "Error dropping database: " . $conn->error . "<br>";
}
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert Student</title>
</head>
<body>
<form action="" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required><br><br>
<label for="marks">Marks:</label>
<input type="number" id="marks" name="marks" required><br><br>
<input type="submit" name="insert" value="Insert">
</form>
</body>
</html>
Output:-

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 125


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

2) Write a web application code using PHP, HTML and MySql for Employees that stores employee’s
id, name, department, designation, etc. in database. Create proper GUI including buttons that run
separate .php file for each database operation like insert, update, delete, select etc.
Program:-
Create Database: employees_db.sql
CREATE DATABASE employees_db;
USE employees_db;
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
department VARCHAR(50) NOT NULL,
designation VARCHAR(50) NOT NULL
);
Step 2: HTML Form
Create a simple HTML form for entering employee data.
File: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 126


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

initial-scale=1.0">
<title>Employee Management</title>
</head>
<body>
<h1>Employee Management System</h1>
<form action="insert.php" method="POST">
<h2>Insert Employee</h2>
<label for="name">Name:</label>
<input type="text" id="name" name="name"

required><br><br>

<label for="department">Department:</label>
<input type="text" id="department" name="department"

required><br><br>

<label for="designation">Designation:</label>
<input type="text" id="designation" name="designation"

required><br><br>

<input type="submit" value="Insert">


</form>
<form action="select.php" method="POST">
<input type="submit" value="View All Employees">
</form>
<form action="update.php" method="POST">
<h2>Update Employee</h2>
<label for="id">Employee ID:</label>
<input type="number" id="id" name="id" required><br><br>
<label for="name">New Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="department">New Department:</label>
<input type="text" id="department"

name="department"><br><br>

<label for="designation">New Designation:</label>


<input type="text" id="designation"

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 127


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

name="designation"><br><br>

<input type="submit" value="Update">


</form>
<form action="delete.php" method="POST">
<h2>Delete Employee</h2>
<label for="id">Employee ID:</label>
<input type="number" id="id" name="id" required><br><br>
<input type="submit" value="Delete">
</form>
</body>
</html>
Step 3: PHP Scripts for CRUD Operations
File: db.php (Database connection)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "employees_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
File: insert.php (Insert Employee)
<?php
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$department = htmlspecialchars($_POST['department']);
$designation = htmlspecialchars($_POST['designation']);
$sql = "INSERT INTO employees (name, department,
designation) VALUES ('$name', '$department', '$designation')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 128


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

}
?>
File: select.php (View Employees)
<?php
include 'db.php';
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table
border='1'><tr><th>ID</th><th>Name</th><th>Department</th><th>De
signation</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"] . "</td><td>" .

$row["name"] . "</td><td>" . $row["department"] . "</td><td>" .


$row["designation"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
File: update.php (Update Employee)
<?php
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = intval($_POST['id']);
$name = htmlspecialchars($_POST['name']);
$department = htmlspecialchars($_POST['department']);
$designation = htmlspecialchars($_POST['designation']);
$sql = "UPDATE employees SET name='$name',
department='$department', designation='$designation' WHERE
id=$id";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 129


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

?>
File: delete.php (Delete Employee)
<?php
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = intval($_POST['id']);
$sql = "DELETE FROM employees WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
}
?>

Output:-

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 130

You might also like