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.
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:
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 );
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()); } ?>
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); ?>
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); ?>
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); ?>
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); ?>
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!