Home >Backend Development >PHP Tutorial >PHP implements many-to-one address book: simple and practical contact management
With the popularity of social networks, people’s social relationships have become more and more complex. Managing contact information is also becoming increasingly important. In this context, it becomes particularly important to develop a simple and practical contact management system. This article will introduce how to use PHP to implement a many-to-one address book to add, delete, modify and search contact information.
Before designing the contact management system, we need to determine the functional modules of the system, which mainly include:
First, we need to design the database table structure of contact information. The following is a simple contact table design:
CREATE TABLE contacts ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, phone VARCHAR(20) NOT NULL, email VARCHAR(50), address VARCHAR(100) );
Next, we use PHP to write the code to implement the above functions. The following is a simple PHP file that contains the functions of adding contacts, deleting contacts, modifying contact information and finding contacts:
<?php // 连接数据库 $host = 'localhost'; $username = 'root'; $password = ''; $database = 'contact_manager'; $conn = new mysqli($host, $username, $password, $database); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 添加联系人 function addContact($name, $phone, $email, $address) { global $conn; $stmt = $conn->prepare("INSERT INTO contacts (name, phone, email, address) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssss", $name, $phone, $email, $address); $stmt->execute(); $stmt->close(); } // 删除联系人 function deleteContact($id) { global $conn; $stmt = $conn->prepare("DELETE FROM contacts WHERE id = ?"); $stmt->bind_param("i", $id); $stmt->execute(); $stmt->close(); } // 修改联系人信息 function updateContact($id, $name, $phone, $email, $address) { global $conn; $stmt = $conn->prepare("UPDATE contacts SET name = ?, phone = ?, email = ?, address = ? WHERE id = ?"); $stmt->bind_param("ssssi", $name, $phone, $email, $address, $id); $stmt->execute(); $stmt->close(); } // 查找联系人 function searchContact($keyword) { global $conn; $stmt = $conn->prepare("SELECT * FROM contacts WHERE name LIKE ? OR phone LIKE ?"); $keyword = "%" . $keyword . "%"; $stmt->bind_param("ss", $keyword, $keyword); $stmt->execute(); $result = $stmt->get_result(); $contacts = array(); while ($row = $result->fetch_assoc()) { $contacts[] = $row; } $stmt->close(); return $contacts; } // 使用示例 addContact("张三", "1234567890", "zhangsan@example.com", "北京市海淀区"); deleteContact(1); updateContact(2, "李四", "0987654321", "lisi@example.com", "上海市浦东新区"); $searchedContacts = searchContact("张"); print_r($searchedContacts); // 关闭数据库连接 $conn->close(); ?>
通过以上的代码示例,我们实现了一个简单实用的联系人管理系统,具有添加、删除、修改和查找联系人的功能。通过不断优化和扩展,我们可以为用户提供更为完善的联系人管理体验。希望这篇文章对您有所帮助,谢谢阅读!
The above is the detailed content of PHP implements many-to-one address book: simple and practical contact management. For more information, please follow other related articles on the PHP Chinese website!