search
HomeBackend DevelopmentPHP ProblemHow to implement many-to-one address book in php

The method to implement a many-to-one address book in php is: 1. Create two database tables, one to store user information and the other to store contact information; 2. In the contact table Add a foreign key field that points to the primary key in the user table to correspond to multiple contacts.

How to implement many-to-one address book in php

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

PHP can implement many-to-one address books by using database operations, object-oriented programming, etc. Among them, many-to-one means that multiple contacts correspond to one user (address book owner).

The implementation method is: create two database tables, one is used to store user information, including user name, password, etc., and the other is used to store contact information, including name, phone number, etc. Add a foreign key field to the Contacts table that points to the primary key in the Users table. In this way, one user can correspond to multiple contacts.

Benefits:

  • Can easily manage and maintain contact information.

  • can improve data security and prevent contact information from being accessed or tampered with by other users.

  • Easy to expand and optimize.

Code example: The following code demonstrates how to use PDO (PHP Data Objects) to implement basic many-to-one address book functionality.

```php
// 创建数据库连接
$db = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8', 'username', 'password');
// 新增用户
$stmt = $db->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$username = 'user1';
$password = 'pass1';
$stmt->execute([$username, $password]);
$user_id = $db->lastInsertId(); // 获取新用户的ID
// 新增联系人
$stmt = $db->prepare("INSERT INTO contacts (name, phone, user_id) VALUES (?, ?, ?)");
$name = 'contact1';
$phone = '1234567890';
$stmt->execute([$name, $phone, $user_id]);
// 查询联系人
$stmt = $db->prepare("SELECT * FROM contacts WHERE user_id = ?");
$user_id = 1; // 假设要查询用户ID为1的联系人
$stmt->execute([$user_id]);
$contacts = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($contacts as $contact) {
    echo "Name: {$contact['name']}, Phone: {$contact['phone']}\n";
}
```

The above is the detailed content of How to implement many-to-one address book in php. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment