"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.
Before building the address book system, we first need to clarify the functional requirements of the system:
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:
CREATE TABLE contacts ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, name VARCHAR(50), phone VARCHAR(20), email VARCHAR(50) );
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50), password VARCHAR(255) – encrypted storage using password hash );
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'); } ?>
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
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 } ?>
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.
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.
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!