ET22BTEC010 - Diya Patel - Practical 11 - PHP Database
ET22BTEC010 - Diya Patel - Practical 11 - PHP Database
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
Practical No:11
}
// 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:-
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,
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>
name="department"><br><br>
name="designation"><br><br>
}
?>
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>" .
?>
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:-