Maging Master ng PHP: How to Create a Simple CRUD App in PHP with MySQL (2025)

PHC-jollibee

Forum Veteran

📣 Introduction​

Kamusta mga kabarkada sa PHCorner! 😄
Dito ako magpo-post ng isang basic CRUD App in PHP para sa mga gustong matuto ng web development gamit ang PHP at MySQL . Hindi kasali dito ang JavaScript o frameworks tulad ng Laravel — pure vanilla PHP lang para mas madali maintindihan.

CRUD stands for:

  • ✅ Create
  • 🔍 Read
  • ✏️ Update
  • 🗑️ Delete

🛠️ What You’ll Learn​

  • Setup database table
  • Connect PHP to MySQL
  • Insert, view, edit, delete records
  • Basic HTML + PHP form handling

🧰 Requirements​

  • XAMPP / WAMP / MAMP (for local server)
  • PHP 7.x or higher
  • MySQL
  • Text Editor (VSCode, Notepad++, etc.)

🗂️ Folder Structure​

htdocs/
└── crud_app/
├── index.php <-- Home / View Records
├── add.php <-- Add Record
├── edit.php <-- Edit Record
└── delete.php <-- Delete Record

Step 1: Create Database and Table​

Open phpMyAdmin , create a new database named crud_db, then run this SQL:

Code:
CREATE TABLE users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

🔌 Step 2: Connect to Database (​

Create a file named config.php:

Code:
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "crud_db";

$conn = mysqli_connect($host, $user, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

📋 Step 3: Display All Records (​

Code:
<?php include 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <title>Simple CRUD App</title>
</head>
<body>
    <h2>Users List</h2>
    <a href="add.php">Add New User</a>
    <table border="1" cellpadding="10">
        <tr>
            <th>ID</th><th>Name</th><th>Email</th><th>Actions</th>
        </tr>
        <?php
        $result = mysqli_query($conn, "SELECT * FROM users ORDER BY id DESC");
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>
                    <td>{$row['id']}</td>
                    <td>{$row['name']}</td>
                    <td>{$row['email']}</td>
                    <td>
                        <a href='edit.php?id={$row['id']}'>Edit</a> |
                        <a href='delete.php?id={$row['id']}' onclick=\"return confirm('Delete this user?')\">Delete</a>
                    </td>
                  </tr>";
        }
        ?>
    </table>
</body>
</html>

📋 Step 3: Display All Records​

Code:
<?php include 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <title>Simple CRUD App</title>
</head>
<body>
    <h2>Users List</h2>
    <a href="add.php">Add New User</a>
    <table border="1" cellpadding="10">
        <tr>
            <th>ID</th><th>Name</th><th>Email</th><th>Actions</th>
        </tr>
        <?php
        $result = mysqli_query($conn, "SELECT * FROM users ORDER BY id DESC");
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>
                    <td>{$row['id']}</td>
                    <td>{$row['name']}</td>
                    <td>{$row['email']}</td>
                    <td>
                        <a href='edit.php?id={$row['id']}'>Edit</a> |
                        <a href='delete.php?id={$row['id']}' onclick=\"return confirm('Delete this user?')\">Delete</a>
                    </td>
                  </tr>";
        }
        ?>
    </table>
</body>
</html>

➕ Step 4: Add New Record​

Code:
<?php
include 'config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];

    mysqli_query($conn, "INSERT INTO users (name, email) VALUES ('$name', '$email')");
    header("Location: index.php");
}
?>

<h2>Add New User</h2>
<form method="post">
    Name: <input type="text" name="name" required><br><br>
    Email: <input type="email" name="email"><br><br>
    <button type="submit">Save</button>
</form>

✏️ Step 5: Edit Record​

Code:
<?php
include 'config.php';
$id = $_GET['id'];
$row = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM users WHERE id='$id'"));

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];

    mysqli_query($conn, "UPDATE users SET name='$name', email='$email' WHERE id='$id'");
    header("Location: index.php");
}
?>

<h2>Edit User</h2>
<form method="post">
    Name: <input type="text" name="name" value="<?= $row['name'] ?>" required><br><br>
    Email: <input type="email" name="email" value="<?= $row['email'] ?>"><br><br>
    <button type="submit">Update</button>
</form>

🗑️ Step 6: Delete Record​

Code:
<?php
include 'config.php';
$id = $_GET['id'];
mysqli_query($conn, "DELETE FROM users WHERE id='$id'");
header("Location: index.php");
?>
Code:

🎉 Final Output​

You should now have:

  • A working list of users
  • Ability to add/edit/delete them
  • Clean and functional interface using only PHP & HTML
 
madali daw ma sql injection ang php kaya yung iba ang gamit na lang is react js o nextjs po? sabi lang po .. ano palagay nyu? aaral lang po ko
 
madali daw ma sql injection ang php kaya yung iba ang gamit na lang is react js o nextjs po? sabi lang po .. ano palagay nyu? aaral lang po ko
it depends on how you code, build and secure your site, bihira nalang sa corporate ang di gumagamit ng framework, so these framework has their own security measures to avoid such.
 

About this Thread

  • 13
    Replies
  • 897
    Views
  • 13
    Participants
Last reply from:
Well1AmNichtDiePerson

Trending Topics

Online now

Members online
412
Guests online
1,323
Total visitors
1,735

Forum statistics

Threads
2,273,561
Posts
28,950,380
Members
1,235,821
Latest member
patpat0115
Back
Top