Creating a basic CRUD (Create, Read, Update, Delete) system in PHP using an object-oriented approach involves defining a class for your data entity (e.g., a database table) and then creating methods within that class to perform the CRUD operations. Below is a simple example of a PHP CRUD system using object-oriented programming:

1. Create a Database and Table:

Before starting with the PHP code, make sure you have a MySQL database set up and a table to work with. For this example, let’s assume you have a “products” table with columns: id (auto-increment), name, description, and price.

2. Create a PHP Class for CRUD Operations:

<?php
class Product {
    private $conn;
    private $table_name = "products";

    public $id;
    public $name;
    public $description;
    public $price;

    public function __construct($db) {
        $this->conn = $db;
    }

    // Create a new product
    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                  SET name=:name, description=:description, price=:price";
        $stmt = $this->conn->prepare($query);

        $this->name = htmlspecialchars(strip_tags($this->name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->price = htmlspecialchars(strip_tags($this->price));

        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":price", $this->price);

        if ($stmt->execute()) {
            return true;
        }

        return false;
    }

    // Read all products
    public function read() {
        $query = "SELECT * FROM " . $this->table_name;
        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }

    // Update a product
    public function update() {
        $query = "UPDATE " . $this->table_name . "
                  SET name=:name, description=:description, price=:price
                  WHERE id=:id";
        $stmt = $this->conn->prepare($query);

        $this->name = htmlspecialchars(strip_tags($this->name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->price = htmlspecialchars(strip_tags($this->price));
        $this->id = htmlspecialchars(strip_tags($this->id));

        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":price", $this->price);
        $stmt->bindParam(":id", $this->id);

        if ($stmt->execute()) {
            return true;
        }

        return false;
    }

    // Delete a product
    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id=:id";
        $stmt = $this->conn->prepare($query);

        $this->id = htmlspecialchars(strip_tags($this->id));
        $stmt->bindParam(":id", $this->id);

        if ($stmt->execute()) {
            return true;
        }

        return false;
    }
}
?>

3. Usage Example:

Here’s how you can use the Product class to perform CRUD operations:

// Include necessary files and create a database connection

// Create a Product object
$product = new Product($db);

// Create a new product
$product->name = "Sample Product";
$product->description = "This is a sample product.";
$product->price = 19.99;
if ($product->create()) {
    echo "Product created successfully.";
}

// Read all products
$stmt = $product->read();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "ID: " . $row['id'] . "<br>";
    echo "Name: " . $row['name'] . "<br>";
    echo "Description: " . $row['description'] . "<br>";
    echo "Price: " . $row['price'] . "<br><br>";
}

// Update a product
$product->id = 1; // Assuming you want to update the product with ID 1
$product->name = "Updated Product Name";
$product->description = "Updated product description.";
$product->price = 24.99;
if ($product->update()) {
    echo "Product updated successfully.";
}

// Delete a product
$product->id = 2; // Assuming you want to delete the product with ID 2
if ($product->delete()) {
    echo "Product deleted successfully.";
}

Remember to replace the database connection code with your own database credentials and connection method (e.g., PDO, MySQLi). Additionally, ensure you have proper error handling and security measures in place in a real-world application.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *