Home  >  Article  >  Backend Development  >  PHP implements many-to-one address book: simple and practical contact management

PHP implements many-to-one address book: simple and practical contact management

王林
王林Original
2024-03-15 12:48:03705browse

PHP implements many-to-one address book: simple and practical contact management

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.

Functional design

Before designing the contact management system, we need to determine the functional modules of the system, which mainly include:

  1. Add contacts: users can enter Contact name, phone number, email and other information to add new contacts.
  2. Delete Contact: Users can delete specified contacts.
  3. Modify contact information: Users can modify the information of existing contacts.
  4. Find a contact: Users can search for information about a specified contact by name or phone number.

Database design

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)
);

PHP code implementation

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!

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