//Connection.
php
<?php
$host="localhost";
$user="root";
$pass="";
$db="db";
$conn=mysqli_connect($host,$user,$pass,$db);
if(!$conn){
die("cnnection failed:".mysqli_connect_error());
}
?>
//ceate database
<?php
require "[Link]";
$sql="CREATE DATABASE db";
if(mysqli_query($conn,$sql)){
echo "created database succefully";
}
else{
echo "failed to created db". mysqli_error($conn);
}
mysqli_close($conn)
?>
//[Link]
<?php
require "[Link]";
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
mysqli_query($conn, "DELETE FROM data WHERE id=$id");
header("Location: [Link]");
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
<style>
body{font-family:Arial;background:#f4f6f8}
.container{width:900px;margin:40px auto;background:#fff;padding:20px;border-radius:8px}
table{width:100%;border-collapse:collapse}
th,td{border:1px solid #ccc;padding:10px;text-align:center}
a{padding:5px 10px;color:white;border-radius:4px;text-decoration:none}
.edit{background:green}
.delete{background:red}
.add{background:#007bff;margin-bottom:10px;display:inline-block}
</style>
</head>
<body>
<div class="container">
<h2>Registered Employees</h2>
<a class="add" href="[Link]">+ Add New</a>
<table>
<tr>
<th>ID</th>
<th>First</th>
<th>Middle</th>
<th>Last</th>
<th>Sex</th>
<th>Email</th>
<th>Action</th>
</tr>
<?php
$result = mysqli_query($conn, "SELECT * FROM data");
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['mname']; ?></td>
<td><?php echo $row['lname']; ?></td>
<td><?php echo $row['sex']; ?></td>
<td><?php echo $row['email']; ?></td>
<td>
<a class="edit" href="[Link]?edit=<?php echo $row['id']; ?>">Edit</a>
<a class="delete" href="?delete=<?php echo $row['id']; ?>"
onclick="return confirm('Delete this record?')">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
<?php mysqli_close($conn); ?>
//[Link]
<?php
require "[Link]";
$id = "";
$fname = $mname = $lname = $sex = $email = "";
$message = "";
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$result = mysqli_query($conn, "SELECT * FROM data WHERE id=$id");
$row = mysqli_fetch_assoc($result);
$fname = $row['fname'];
$mname = $row['mname'];
$lname = $row['lname'];
$sex = $row['sex'];
$email = $row['email'];
}
if (isset($_POST['save'])) {
$fname = $_POST['fname'];
$mname = $_POST['mname'];
$lname = $_POST['lname'];
$sex = $_POST['sex'];
$email = $_POST['email'];
$sql = "INSERT INTO data (fname,mname,lname,sex,email)
VALUES ('$fname','$mname','$lname','$sex','$email')";
if (mysqli_query($conn, $sql)) {
header("Location: [Link]");
exit();
}
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$fname = $_POST['fname'];
$mname = $_POST['mname'];
$lname = $_POST['lname'];
$sex = $_POST['sex'];
$email = $_POST['email'];
$sql = "UPDATE data SET
fname='$fname',
mname='$mname',
lname='$lname',
sex='$sex',
email='$email'
WHERE id=$id";
if (mysqli_query($conn, $sql)) {
header("Location: [Link]");
exit();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register Employee</title>
<style>
body{font-family:Arial;background:#f4f6f8}
.box{width:400px;margin:50px auto;background:#fff;padding:20px;border-radius:8px}
input,select,button{width:100%;padding:10px;margin-bottom:10px}
button{background:#007bff;color:white;border:none}
</style>
</head>
<body>
<div class="box">
<h2><?php echo $id ? "Edit Employee" : "Register Employee"; ?></h2>
<form method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="text" name="fname" placeholder="First Name" value="<?php echo $fname; ?>"
required>
<input type="text" name="mname" placeholder="Middle Name" value="<?php echo $mname; ?>"
required>
<input type="text" name="lname" placeholder="Last Name" value="<?php echo $lname; ?>"
required>
<select name="sex" required>
<option value="">Select Sex</option>
<option <?php if($sex=="Male") echo "selected"; ?>>Male</option>
<option <?php if($sex=="Female") echo "selected"; ?>>Female</option>
</select>
<input type="email" name="email" placeholder="Email" value="<?php echo $email; ?>" required>
<?php if ($id) { ?>
<button type="submit" name="update">Update</button>
<?php } else { ?>
<button type="submit" name="save">Save</button>
<?php } ?>
</form>
<a href="[Link]">View Employees</a>
</div>
</body>
</html>
//[Link]
<?php
require "[Link]";
$sql = "CREATE TABLE data (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
fname VARCHAR(200) NOT NULL,
mname VARCHAR(200) NOT NULL,
lname VARCHAR(200) NOT NULL,
sex VARCHAR(10) NOT NULL,
email VARCHAR(200) NOT NULL UNIQUE
)";
if (mysqli_query($conn, $sql)) {
echo "Success: Table created successfully!";
} else {
echo "Failed to create table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>