Home > Backend Development > PHP Tutorial > Starting from scratch: Detailed explanation of implementing a many-to-one address book system in PHP

Starting from scratch: Detailed explanation of implementing a many-to-one address book system in PHP

WBOY
Release: 2024-03-16 09:16:01
Original
621 people have browsed it

Starting from scratch: Detailed explanation of implementing a many-to-one address book system in PHP

Starting from scratch: Detailed explanation of implementing a many-to-one address book system in PHP

With the popularity of the Internet and the development of social networks, the way people communicate in daily life Become diverse. An address book system that facilitates the management of contact information is becoming increasingly important. In this article, we will use PHP language to implement a simple and practical many-to-one address book system from scratch. Through this example, we will learn how to design the database table structure and write PHP code to implement basic functions such as addition, deletion, modification, and query.

1. Database design

First, we need to design the database table structure required by the address book system. In this simple example, we only need one table to store contact information. Create a table named "contacts", including the following fields:

  • id: primary key, self-increasing unique identifier
  • name: contact name
  • phone: Contact phone number
  • email: Contact email

Use the following SQL statement to create this table:

CREATE TABLE contacts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    phone VARCHAR(15) NOT NULL,
    email VARCHAR(50) NOT NULL
);
Copy after login

2. Connect to the database

Next, we need to write a PHP file to connect to the database, so that we can reference this file in other files in the system to facilitate connection to the database. Create a file named "db_connect.php" and write the following code:

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "my_contacts";

$conn = mysqli_connect($host, $username, $password, $database);

if (!$conn) {
    die("Failed to connect to database: " . mysqli_connect_error());
}
?>
Copy after login

3. Add contacts

Now, we can start to implement the functions of the address book system. First, we write a PHP file "add_contact.php" to add contacts, which is used to insert new contact information into the database. Here is a code example:

<?php
include 'db_connect.php';

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];

$sql = "INSERT INTO contacts (name, phone, email) VALUES ('$name', '$phone', '$email')";
if (mysqli_query($conn, $sql)) {
    echo "Contact added successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
Copy after login

4. Delete Contact

Next, we write a PHP file "delete_contact.php" to delete a contact, which is used to delete the contact information of the specified ID from the database . Here is a code example:

<?php
include 'db_connect.php';

$id = $_POST['id'];

$sql = "DELETE FROM contacts WHERE id = $id";
if (mysqli_query($conn, $sql)) {
    echo "Contact deleted successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
Copy after login

5. Modify Contact

Continue, we write a PHP file "update_contact.php" that modifies contact information, which is used to update the contact information with the specified ID in the database. The following is a code example:

<?php
include 'db_connect.php';

$id = $_POST['id'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];

$sql = "UPDATE contacts SET name='$name', phone='$phone', email='$email' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
    echo "Contact information updated successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
Copy after login

6. Query Contacts

Finally, we write a PHP file "get_contacts.php" to query contact information, which is used to obtain all contact information from the database and return it to the front-end page. Here is a code example:

<?php
include 'db_connect.php';

$sql = "SELECT * FROM contacts";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row['id'] . ", name: " . $row['name'] . ", phone: " . $row['phone'] . ", email: " . $row ['email'] . "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>
Copy after login

Through the above steps, we have implemented a simple many-to-one address book system. Through this example, we learned how to design the database table structure, connect to the database, and implement basic functions such as addition, deletion, modification, and query. In practical applications, we can extend functions based on this example to improve the integrity and practicality of the system. I hope this article can be helpful to beginners and make everyone more proficient in using PHP to build practical network applications.

The above is the detailed content of Starting from scratch: Detailed explanation of implementing a many-to-one address book system in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template