Register Now

Login


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

PHP (PHP: Hypertext Preprocessor) Address Book

It is an ideal language for developing PHP (PHP: Hypertext Preprocessor) Address Book, as it can be used to store and manage contact information such as names, addresses, phone numbers, emails, and more. PHP can also be used to create user interfaces for users to add, edit, and delete contacts. Additionally, it can be used to create automated emails and notifications, such as birthday reminders and contact updates.

PHP (PHP: Hypertext Preprocessor) Address Book

Address Book application that is built using PHP and MySQL:

An address book application in PHP and MySQL can be created by using the following steps:

  1. Create a database in MySQL and create a table named “contacts” with the following columns: id (primary key), name, email, phone, address.
  2. Create a new PHP file named “config.php” and connect to the MySQL database using the following code:
<?php
$host = "localhost";
$user = "username";
$password = "password";
$db = "address_book";

$conn = mysqli_connect($host, $user, $password, $db);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>
  1. Create a new PHP file named “add.php” and use the following code to insert new contacts into the “contacts” table:
<?php
include 'config.php';

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];

    $sql = "INSERT INTO contacts (name, email, phone, address) VALUES ('$name', '$email', '$phone', '$address')";

    if (mysqli_query($conn, $sql)) {
        echo "New contact added successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}
?>
  1. Create a new PHP file named “view.php” and use the following code to retrieve and display all the contacts from the “contacts” table:
<?php
include 'config.php';

$sql = "SELECT id, name, email, phone, address FROM contacts";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. " - Phone: " . $row["phone"]. " - Address: " . $row["address"]. "<br>";
    }
} else {
    echo "0 results";
}
  1. Create a simple HTML form for adding new contacts to the address book and linking it to the “add.php” file, and create a link to the “view.php” file to view all the contacts.

You can also add more functionality like editing and deleting contacts, searching for a specific contact, and validating form inputs for security.

It’s worth noting that this is a basic example and it may not include all the necessary security measures and best practices for building a production-ready application, it’s recommendable to use prepared statements, validation and sanitization functions to prevent SQL injection attacks and data breaches.


Leave a reply