Home > Article > Backend Development > php add, delete, modify, check mvc
PHP Add, Delete, Modify and Check Applications in MVC Mode
With the continuous development of Web applications, more and more developers choose to use PHP language as their development language. Therefore, it is essential to understand how to use PHP to perform basic add, delete, modify, and query operations. In this article, I will explore how to use PHP to perform add, delete, modify and query operations in MVC (model-view-controller) mode.
What is the MVC pattern?
In the MVC pattern, the application is divided into three parts: Model (Model), View (View) and Controller (Controller). The specific functions of these parts are as follows:
Why use MVC pattern?
There are many benefits of using the MVC pattern, including the following:
How to use PHP to perform add, delete, modify and check operations in MVC mode?
Now, let us consider how to use PHP to perform add, delete, modify and query operations in MVC mode. We'll first discuss how to set up a database connection and DAO, and then move on to how to write models, controllers, and views.
Setting up a database connection in PHP is very simple. We can do this using PHP's mysqli and PDO extensions. In this article, we will use the PDO extension.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Once we have established the database connection, we can use DAO to interact with the database. DAO is a general object used to handle database connections and queries. We will use a BaseDAO class to achieve this.
class BaseDAO {
protected $conn; function __construct() { $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $this->conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } } function execute($sql) { $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); return $result; }
}
Now, we can use the DAO set up above to write the model. In the MVC pattern, the model contains all the logic related to data storage and processing as follows:
class UserModel extends BaseDAO {
function getUser($userId) { $sql = "SELECT * FROM users WHERE id = $userId"; $result = $this->execute($sql); return $result; } function getUsers() { $sql = "SELECT * FROM users"; $result = $this->execute($sql); return $result; } function addUser($username, $password) { $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; $result = $this->execute($sql); return $result; } function updateUser($userId, $username, $password) { $sql = "UPDATE users SET username = '$username', password = '$password' WHERE id = $userId"; $result = $this->execute($sql); return $result; } function deleteUser($userId) { $sql = "DELETE FROM users WHERE id = $userId"; $result = $this->execute($sql); return $result; }
}
The above code shows A UserModel, which contains getUser (get a user by Id), getUsers (get all users), addUser (add a new user), updateUser (update user password and user name) and deleteUser (delete a user) functions. These functions are written according to our needs and can be modified according to specific needs in actual projects.
The controller connects the model and view and handles any requests from the user. In the MVC pattern, the controller is the main logic of the application and is usually the entry point to the user interface. As shown below:
class UserController {
function getUser($userId) { $userModel = new UserModel(); $result = $userModel->getUser($userId); return $result; } function getUsers() { $userModel = new UserModel(); $result = $userModel->getUsers(); return $result; } function addUser($username, $password) { $userModel = new UserModel(); $result = $userModel->addUser($username, $password); return $result; } function updateUser($userId, $username, $password) { $userModel = new UserModel(); $result = $userModel->updateUser($userId, $username, $password); return $result; } function deleteUser($userId) { $userModel = new UserModel(); $result = $userModel->deleteUser($userId); return $result; }
}
The above code shows a UserController, which contains getUser, getUsers, addUser, updateUser and deleteUser functions. These functions will be called when requested by the user.
In the MVC pattern, the view is the appearance and user interaction of the application. We will use HTML templates to implement the views and PHP to dynamically render the data. As shown below:
<title>Users</title>
<?php $userController = new UserController(); $users = $userController->getUsers(); foreach ($users as $user) { echo "ID: " . $user['id'] . "<br />"; echo "Name: " . $user['username'] . "<br />"; echo "Password: " . $user['password'] . "<br />"; echo "<br />"; } ?>
body>
The above code gets all users from the user controller and presents them in the form of a table in the HTML template.
Summary
In this article, we have an in-depth exploration of how to use PHP to perform add, delete, modify and query operations in MVC mode. We used PDO extensions to implement database connections and DAO, and created a BaseDAO class to handle all database connections and queries. We then wrote a UserModel to handle the model logic and a UserController to coordinate all operations. Finally, we wrote an HTML template to present the data and interact with the user. These steps make up a complete MVC pattern-based application that can be used to implement your specific needs.
The above is the detailed content of php add, delete, modify, check mvc. For more information, please follow other related articles on the PHP Chinese website!