DBMS Lab
<?php
include 'db.php';
$sql = "CREATE TABLE IF NOT EXISTS Employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
position VARCHAR(50) NOT NULL,
salary DECIMAL(10, 2) NOT NULL,
department VARCHAR(50) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table Employees created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
-----------------------------------------------------------------------------------------------------------------
<?php
$servername = "localhost";
$username = "tanush"; // Replace with your MySQL username
$password = "mypassword"; // Replace with your MySQL password
$dbname = "employee_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
-----------------------------------------------------------------------------------------------------------------
<?php
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST['id'];
$sql = "DELETE FROM Employees WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "Employee deleted successfully";
} else {
echo "Error deleting employee: " . $conn->error;
}
}
?>
-----------------------------------------------------------------------------------------------------------------
<form method="post" action="">
ID: <input type="number" name="id" required><br>
<input type="submit" value="Delete Employee">
</form>
<!DOCTYPE html>
<html>
<head>
<title>Employee Management</title>
</head>
<body>
<h1>Employee Management System</h1>
<ul>
<li><a href="create.php">Create Table</a></li>
<li><a href="insert.php">Insert Employee</a></li>
<li><a href="select.php">View Employees</a></li>
<li><a href="update.php">Update Employee</a></li>
<li><a href="delete.php">Delete Employee</a></li>
</ul>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------
<?php
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$position = $_POST['position'];
$salary = $_POST['salary'];
$department = $_POST['department'];
$sql = "INSERT INTO Employees (name, position, salary, department) VALUES ('$name',
'$position', $salary, '$department')";
if ($conn->query($sql) === TRUE) {
echo "New employee added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
<form method="post" action="">
Name: <input type="text" name="name" required><br>
Position: <input type="text" name="position" required><br>
Salary: <input type="number" step="0.01" name="salary" required><br>
Department: <input type="text" name="department" required><br>
<input type="submit" value="Add Employee">
</form>
-----------------------------------------------------------------------------------------------------------------
<?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>Position</th>
<th>Salary</th>
<th>Department</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['position']}</td>
<td>{$row['salary']}</td>
<td>{$row['department']}</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
-----------------------------------------------------------------------------------------------------------------
<?php
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = $_POST['id'];
$name = $_POST['name'];
$position = $_POST['position'];
$salary = $_POST['salary'];
$department = $_POST['department'];
$sql = "UPDATE Employees SET name='$name', position='$position', salary=$salary,
department='$department' WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "Employee updated successfully";
} else {
echo "Error updating employee: " . $conn->error;
}
}
?>
-----------------------------------------------------------------------------------------------------------------
<form method="post" action="">
ID: <input type="number" name="id" required><br>
Name: <input type="text" name="name" required><br>
Position: <input type="text" name="position" required><br>
Salary: <input type="number" step="0.01" name="salary" required><br>
Department: <input type="text" name="department" required><br>
<input type="submit" value="Update Employee">
</form>