Home  >  Article  >  Backend Development  >  Create a CRUD application using PHP and MySQL

Create a CRUD application using PHP and MySQL

PHPz
PHPzOriginal
2023-05-11 15:33:121589browse

With the development of the Internet, Web applications have become a necessity in our daily life and work. With the help of PHP and MySQL, we can easily create a web-based application that allows users to add, delete, modify and view data. This article will introduce how to use PHP and MySQL to create a simple CRUD application.

  1. Create database and tables

First, we need to create a database and a related table. Open PHPMyAdmin or other MySQL management tools, create a new database, name it "crud", and set the character set to "utf8mb4_general_ci".

Next, we need to create a table to store our data. Let's assume we want to create a simple user management system with the following fields: id, username, email address, and password. We can use the following SQL statement to create this table:

CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL,
email varchar(100) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

  1. Connect to the database

Now, we have After creating the database and tables, we need to connect to the database. In PHP, we can use mysqli or PDO extension to implement database connection. Here we will use the mysqli extension. Here is sample code to connect to the database:

093c8836a0c1e9e4e3932634077fd48econnect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connection successful";
?> ;

  1. Display data

Now that we are connected to the database, we can start writing code to display the data. The following is sample code to display user data:

f61c171652bbb8054ac09cdd685d7c29query($sql);

if ($result->num_rows > 0) {
// Output data
while($row = $result->fetch_assoc()) {

echo "id: " . $row["id"]. " - 用户名: " . $row["username"]. " - 邮箱地址: " . $row["email"]. "<br>";

}
} else {
echo "0 result";
}
$conn->close();
?>

  1. Add data

Now that we know how to display data, we can write code to add data. Here is sample code to add user data:

a0b8e6bfa75d7767f2dd6ae2165333edquery($sql) === TRUE) {

echo "新记录添加成功";

} else {

echo "错误: " . $sql . "<br>" . $conn->error;

}

$conn->close();
}
?>

  1. Update data

Now that we know how to add data, we can write code to update the data. Here is sample code to update user data:

93449a8fb7343453c258214b968b1017query($ sql) === TRUE) {

echo "记录更新成功";

} else {

echo "错误: " . $sql . "<br>" . $conn->error;

}

$conn->close();
}
?> ;

  1. Delete data

Finally, we need to know how to delete data. Here is sample code to delete user data:

22f54110cdd52ebeddfc4743518ca489query($sql) === TRUE) {

echo "记录删除成功";

} else {

echo "错误: " . $sql . "<br>" . $conn->error;

}

$conn->close();
}
?>

We already know how to create A basic CRUD application. Of course, this is just a simple example. In practical applications, we need to carry out more functional expansion and implementation according to actual needs.

The above is the detailed content of Create a CRUD application using PHP and MySQL. 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