Home >Backend Development >PHP Tutorial >Building a Many-to-One Address Book System: A Practical Guide to PHP Programming

Building a Many-to-One Address Book System: A Practical Guide to PHP Programming

WBOY
WBOYOriginal
2024-03-15 12:06:04997browse

Building a Many-to-One Address Book System: A Practical Guide to PHP Programming

"Building a Many-to-One Address Book System: A Practical Guide to PHP Programming"

With the development of the Internet, address book systems are becoming more and more important in daily life important. People need a convenient and fast way to manage their contact information so they can find and contact them at any time. This article will introduce how to use the PHP programming language to build a many-to-one address book system to help readers implement the management and search functions of contact information.

1. System requirements analysis

Before building the address book system, we first need to clarify the functional requirements of the system:

  • Users can add, edit, and delete contacts Person information;
  • Users can search contact information based on name, phone number and other keywords;
  • The system needs to be able to support multiple users, and each user has his or her own independent address book;
  • Users need to log in to access their address book.

2. Database design

In order to store contact information and user information, we need to design two data tables: contacts (contact table) and users (user table). The following is their structural design:

2.1. Contacts table structure design

CREATE TABLE contacts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    name VARCHAR(50),
    phone VARCHAR(20),
    email VARCHAR(50)
);

2.2. users table structure design

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50),
    password VARCHAR(255) – encrypted storage using password hash
);

3. PHP programming implementation

3.1. User login function

First, we need to implement the user login function. Create a login.php file containing the following code:

<?php
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
    //Process user login logic
    $username = $_POST['username'];
    $password = $_POST['password'];

    //Verify username and password
    // Query whether the user exists in the database and whether the password is correct

    // If the verification passes, set the session and jump to the address book page
    $_SESSION['username'] = $username;
    header('Location: contacts.php');
}
?>

3.2. Address book page

Create a contacts.php file to display the user's address book information. Contains the following code:

<?php
session_start();
if(!isset($_SESSION['username'])) {
    header('Location: login.php');
}

// Query the current user's contact information
//Query the contacts table based on user ID

// Display the contact information list

3.3. Add contact function

In the contacts.php file, add a form for adding contact information. The code example is as follows:

<form method="post" action="add_contact.php">
    <input type="text" name="name" placeholder="Name">
    <input type="text" name="phone" placeholder="phone number">
    <input type="text" name="email" placeholder="email">
    <button type="submit">Add Contact</button>
</form>

Create the add_contact.php file to handle the logic of adding contacts. The code example is as follows:

<?php
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
    //Process the logic of adding contacts
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    
    //Insert contact information into the database
}
?>

3.4. Edit and delete contact functions

In the contacts.php file, add edit and delete buttons for each contact information, and correspond to edit_contact.php and delete_contact respectively .php file processing.

3.5. Search function

Add a search box on the address book page, and users can enter keywords to search. Create a search.php file, query contact information based on keywords and display it on the page.

4. Summary

Through the above steps, we successfully built a many-to-one address book system. Readers can expand and optimize the system according to their own needs and preferences, such as adding grouping functions, exporting contact information, synchronizing to mobile phones, etc. I hope this article can help readers better understand and practice PHP programming and build more interesting and practical web applications.

The above is the detailed content of Building a Many-to-One Address Book System: A Practical Guide to PHP Programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn