CRUD PHP New
Posted on April 1, 2024 (Last modified on October 11, 2024) • 3 min read • 604 wordsVideo is in Swedish
In this article, we will explore how to create a basic CRUD application using PHP. CRUD is an acronym that stands for Create, Read, Update, and Delete, which are the four primary operations performed on data in a database.
CRUD is a fundamental concept in software development, particularly in web applications. It refers to the ability of a system to perform basic operations on data stored in a database. These operations include:
To create a CRUD application in PHP, we will use a simple example of a “To-Do List” application. We will create a table to store tasks, and then implement the CRUD operations using PHP.
First, we need to create a database table to store our tasks. We can do this by running the following SQL query:
CREATE TABLE tasks (
id INT PRIMARY KEY,
task VARCHAR(255),
status VARCHAR(10)
);
Next, we will create the PHP code for each CRUD operation.
To create a new task, we can use the following PHP code:
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Get the task from the form
$task = $_POST["task"];
// Insert the task into the database
$query = "INSERT INTO tasks (task, status) VALUES ('$task', 'pending')";
mysqli_query($conn, $query);
// Close the connection
mysqli_close($conn);
?>
To retrieve all tasks from the database, we can use the following PHP code:
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Retrieve all tasks from the database
$query = "SELECT * FROM tasks";
$result = mysqli_query($conn, $query);
// Display the tasks
while ($row = mysqli_fetch_assoc($result)) {
echo $row["id"] . ": " . $row["task"] . "<br>";
}
// Close the connection
mysqli_close($conn);
?>
To update a task in the database, we can use the following PHP code:
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Get the task ID and new status from the form
$id = $_POST["id"];
$status = $_POST["status"];
// Update the task in the database
$query = "UPDATE tasks SET status='$status' WHERE id=$id";
mysqli_query($conn, $query);
// Close the connection
mysqli_close($conn);
?>
To delete a task from the database, we can use the following PHP code:
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Get the task ID from the form
$id = $_POST["id"];
// Delete the task from the database
$query = "DELETE FROM tasks WHERE id=$id";
mysqli_query($conn, $query);
// Close the connection
mysqli_close($conn);
?>
In this article, we have created a basic CRUD application in PHP using a simple example of a “To-Do List” application. We have implemented the Create, Read, Update, and Delete operations using PHP and MySQL. This is just a starting point, and you can expand on this example to create more complex applications.
Remember to always follow best practices for security and error handling when building web applications.
Swedish