First, let's create the database and table to store the contact information. We will use a database called "contacts" and a table called "contact_list". The table will have two columns: "name" and "phone".
Copy CREATE DATABASE contacts;
USE contacts;
CREATE TABLE contact_list (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL
);
Next, let's create the HTML form to add a new contact. We'll create a file called "create.php".
Copy <!DOCTYPE html>
<html>
<head>
<title>Add Contact</title>
<style>
input[type=text], input[type=tel] {
margin-bottom: 10px;
padding: 5px;
font-size: 16px;
border-radius: 5px;
border: none;
border: 1px solid #ccc;
width: 100%;
box-sizing: border-box;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
a {
color: #4CAF50;
text-decoration: none;
}
</style>
</head>
<body>
<h2>Add Contact</h2>
<form action="process.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" required>
<input type="submit" value="Add Contact">
</form>
<br>
<a href="show.php">show Contact</a>
</body>
</html>
Now, let's create the PHP script to process the form and insert the new contact into the database. We'll create a file called "process.php".
Copy <?php
include('dbconfig.php');
$name = $_POST['name'];
$phone = $_POST['phone'];
$sql = "INSERT INTO contact_list (name, phone) VALUES ('$name', '$phone')";
if (mysqli_query($conn, $sql)) {
echo "New contact added successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
// redirect to the show page
header('Location: show.php');
?>
Next, let's create the "show.php" page to display the list of contacts.
Copy <!DOCTYPE html>
<html>
<head>
<title>Contact List</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
th {
background-color: #4CAF50;
color: white;
}
a {
color: #4CAF50;
text-decoration: none;
}
</style>
</head>
<body>
<h2>Contact List</h2>
<table>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
include('dbconfig.php');
$sql = "SELECT * FROM contact_list";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["phone"]; ?></td>
<td><a href="update_form.php?id=<?php echo $row["id"]; ?>">Edit</a></td>
<td><a href="delete.php?id=<?php echo $row["id"]; ?>" onclick="return confirm('Are you sure you want to delete this contact?')">Delete</a></td>
<!-- <td><a href="delete.php?id=<?php echo $row['id']; ?>" class="btn btn-danger">Delete</a></td> -->
</tr>
<?php
}
} else {
echo "<tr><td colspan='4'>No contacts found</td></tr>";
}
mysqli_close($conn);
?>
</table>
<br>
<a href="create.php">Add Contact</a>
</body>
</html>
This code fetches all the contacts from the database and displays them in a table with columns for name, contact number, edit button and delete button for each contact. The edit and delete buttons are links that redirect the user to the update_form.php and delete.php pages respectively, with the contact's ID passed as a parameter in the URL.
Next, let's create code for the update functionality
Copy <?php
include('dbconfig.php');
if(isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM contact_list WHERE id=$id";
$result = mysqli_query($conn, $sql);
if($result && mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$phone = $row['phone'];
} else {
echo "No record found";
exit;
}
} else {
echo "Invalid request";
exit;
}
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$sql = "UPDATE contact_list SET name='$name', phone='$phone' WHERE id=$id";
if(mysqli_query($conn, $sql)) {
header("Location: show.php");
exit;
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Contact</title>
<style>
form {
display: inline-block;
margin: 20px;
}
input[type=text], input[type=tel] {
padding: 10px;
border-radius: 5px;
border: none;
margin-bottom: 10px;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Update Contact</h2>
<form method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>">
<br>
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" value="<?php echo $phone; ?>">
<br>
<input type="submit" name="submit" value="Update">
</form>
<a href="show.php">Back to Contact List</a>
</body>
</html>
And finally let's code for deleting a contact
Copy <?php
include('dbconfig.php');
$id = $_GET['id'];
$sql = "DELETE FROM contact_list WHERE id=$id";
if(mysqli_query($conn, $sql)) {
header('location: show.php');
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>