Skip to main content

PHP CRUD with custom Alert.

 Database structure:

database:testing


CREATE TABLE users (

    id INT AUTO_INCREMENT PRIMARY KEY,

    name VARCHAR(50) NOT NULL,

    email VARCHAR(50) NOT NULL

);









<?php

// Database connection

$host = 'localhost';

$dbname = 'testing';

$username = 'root';

$password = '';


try {

    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

    die("Database connection failed: " . $e->getMessage());

}


// Initialize variables

$name = $email = '';

$message = '';

$action = 'add'; // Default action

$id = ''; // For update operation


// Create (Insert) or Update

if (isset($_POST['submit'])) {

    $name = $_POST['name'];

    $email = $_POST['email'];


    if (!empty($name) && !empty($email)) {

        if ($_POST['action'] === 'add') {

            // Insert new record

            $stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (:name, :email)");

            $stmt->execute([':name' => $name, ':email' => $email]);

            $message = "User added successfully!";

        } elseif ($_POST['action'] === 'update') {

            // Update existing record

            $id = $_POST['id'];

            $stmt = $conn->prepare("UPDATE users SET name = :name, email = :email WHERE id = :id");

            $stmt->execute([':name' => $name, ':email' => $email, ':id' => $id]);

            $message = "User updated successfully!";

        }

        // Redirect to index.php with message as query parameter

        header("Location: index.php?message=" . urlencode($message));

        exit();

    } else {

        $message = "Please fill all fields!";

    }

}


// Delete

if (isset($_GET['delete'])) {

    $id = $_GET['delete'];

    try {

        $stmt = $conn->prepare("DELETE FROM users WHERE id = :id");

        $stmt->execute([':id' => $id]);

        $message = "User deleted successfully!";

        // Redirect to index.php with message as query parameter

        header("Location: index.php?message=" . urlencode($message));

        exit();

    } catch (PDOException $e) {

        $message = "Error deleting user: " . $e->getMessage();

    }

}


// Fetch data for update

if (isset($_GET['edit'])) {

    $id = $_GET['edit'];

    $stmt = $conn->prepare("SELECT * FROM users WHERE id = :id");

    $stmt->execute([':id' => $id]);

    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($user) {

        $name = $user['name'];

        $email = $user['email'];

        $action = 'update';

    }

}


// Read (Fetch all users)

$stmt = $conn->prepare("SELECT * FROM users");

$stmt->execute();

$users = $stmt->fetchAll(PDO::FETCH_ASSOC);


// Get message from query parameter

if (isset($_GET['message'])) {

    $message = urldecode($_GET['message']);

}

?>


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>PHP CRUD with Custom Alert</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            background-color: #f4f4f4;

            display: flex;

            flex-direction: column;

            align-items: center;

            padding: 20px;

        }


        form {

            background: white;

            padding: 20px;

            border-radius: 10px;

            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);

            margin-bottom: 20px;

        }


        input[type="text"], input[type="email"] {

            padding: 10px;

            margin: 5px 0;

            width: 100%;

            border: 1px solid #ccc;

            border-radius: 5px;

        }


        button {

            padding: 10px 20px;

            font-size: 16px;

            cursor: pointer;

            border: none;

            background-color: #007bff;

            color: white;

            border-radius: 5px;

        }


        button:hover {

            background-color: #0056b3;

        }


        table {

            width: 100%;

            border-collapse: collapse;

            margin-top: 20px;

        }


        table, th, td {

            border: 1px solid #ddd;

        }


        th, td {

            padding: 10px;

            text-align: left;

        }


        th {

            background-color: #007bff;

            color: white;

        }


        tr:nth-child(even) {

            background-color: #f2f2f2;

        }


        .alert {

            display: none;

            position: fixed;

            top: 0;

            left: 0;

            width: 100%;

            height: 100%;

            background-color: rgba(0, 0, 0, 0.5);

            justify-content: center;

            align-items: center;

        }


        .alert-content {

            background-color: white;

            padding: 20px;

            border-radius: 10px;

            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);

            text-align: center;

            width: 300px;

        }


        .close-btn {

            float: right;

            font-size: 24px;

            font-weight: bold;

            cursor: pointer;

        }


        .close-btn:hover {

            color: #ff0000;

        }


        .alert-buttons {

            margin-top: 20px;

        }


        .alert-buttons button {

            margin: 0 10px;

            padding: 10px 20px;

            font-size: 16px;

            cursor: pointer;

            border: none;

            border-radius: 5px;

        }


        .alert-buttons button.yes {

            background-color: #28a745;

            color: white;

        }


        .alert-buttons button.yes:hover {

            background-color: #218838;

        }


        .alert-buttons button.no {

            background-color: #dc3545;

            color: white;

        }


        .alert-buttons button.no:hover {

            background-color: #c82333;

        }


        h1{

            color:red;font-weight: bold;

        }

    </style>

</head>

<body>

    <h1>PHP CRUD with Custom Alert</h1>


    <!-- Form for adding/updating users -->

    <form method="POST" action="index.php">

        <input type="hidden" name="id" value="<?php echo $id; ?>">

        <input type="hidden" name="action" value="<?php echo $action; ?>">

        <input type="text" name="name" placeholder="Name" value="<?php echo $name; ?>" required>

        <input type="email" name="email" placeholder="Email" value="<?php echo $email; ?>" required>

        <button type="submit" name="submit">

            <?php echo $action === 'add' ? 'Add User' : 'Update User'; ?>

        </button>

    </form>


    <!-- Display users -->

    <table>

        <thead>

            <tr>

                <th>ID</th>

                <th>Name</th>

                <th>Email</th>

                <th>Actions</th>

            </tr>

        </thead>

        <tbody>

            <?php foreach ($users as $user): ?>

                <tr>

                    <td><?php echo $user['id']; ?></td>

                    <td><?php echo $user['name']; ?></td>

                    <td><?php echo $user['email']; ?></td>

                    <td>

                        <a href="index.php?edit=<?php echo $user['id']; ?>">Edit</a>

                        <a href="#" onclick="confirmDelete(<?php echo $user['id']; ?>)">Delete</a>

                    </td>

                </tr>

            <?php endforeach; ?>

        </tbody>

    </table>


    <!-- Success/Error Alert -->

    <div id="customAlert" class="alert">

        <div class="alert-content">

            <span class="close-btn" onclick="closeAlert()">&times;</span>

            <h2>Alert!</h2>

            <p id="alertMessage"><?php echo $message; ?></p>

            <button id="okBtn" onclick="closeAlert()">OK</button>

        </div>

    </div>


    <!-- Delete Confirmation Alert -->

    <div id="deleteAlert" class="alert">

        <div class="alert-content">

            <span class="close-btn" onclick="closeDeleteAlert()">&times;</span>

            <h2>Confirm Delete</h2>

            <p>Are you sure you want to delete this user?</p>

            <div class="alert-buttons">

                <button class="yes" onclick="deleteUser()">Yes</button>

                <button class="no" onclick="closeDeleteAlert()">No</button>

            </div>

        </div>

    </div>


    <script>

        // Show success/error alert if there's a message from PHP

        const urlParams = new URLSearchParams(window.location.search);

        const alertMessage = urlParams.get('message');


        if (alertMessage) {

            document.getElementById('customAlert').style.display = 'flex';

            document.getElementById('alertMessage').textContent = alertMessage;

        }


        // Close success/error alert

        function closeAlert() {

            document.getElementById('customAlert').style.display = 'none';

        }


        // Delete confirmation

        let userIdToDelete = null;


        function confirmDelete(userId) {

            userIdToDelete = userId;

            document.getElementById('deleteAlert').style.display = 'flex';

        }


        function closeDeleteAlert() {

            document.getElementById('deleteAlert').style.display = 'none';

        }


        function deleteUser() {

            if (userIdToDelete) {

                window.location.href = `index.php?delete=${userIdToDelete}`;

            }

        }

    </script>

</body>

</html>



Download Source Code:

Download

Comments

Popular posts from this blog

Microsoft Excel top functions.

  Microsoft Excel offers numerous functions to simplify data analysis and calculation. Here are some of the most commonly used and powerful functions: Basic Functions SUM : Adds values. =SUM(A1:A10) AVERAGE : Calculates the mean of numbers. =AVERAGE(A1:A10) IF : Performs logical tests and returns values based on conditions. =IF(A1>10, "Yes", "No") COUNT : Counts numeric values in a range. =COUNT(A1:A10) LEN : Returns the length of a text string. =LEN(A1) Lookup and Reference Functions VLOOKUP : Looks for a value in the first column and returns a value in the same row from another column. =VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup]) HLOOKUP : Similar to Here are practical examples of common Excel functions and how they can be applied in real-life scenarios: 1. SUM Scenario: Calculate the total sales for a week. Example: A B Day Sales Monday 200 Tuesday 150 Wednesday 300 Thursday 250 Friday 100 ...

Creating Wages Sheet using Microsoft Excel

  Creating wages sheet using Microsoft Excel The DAYS360 function in Microsoft Excel calculates the number of days between two dates based on a 360-day year (commonly used in financial calculations). A 360-day year assumes each month has 30 days, simplifying interest and payment schedules. Syntax: DAYS360(start_date, end_date, [method]) Parameters: start_date : The starting date (required). Enter it as a valid date or reference a cell containing a date. end_date : The ending date (required). Enter it similarly to the start_date. method : Logical value (optional). FALSE (default): Uses the U.S. (NASD) method for date calculations. TRUE : Uses the European method. Key Differences Between Methods: U.S. (NASD) : Adjusts the start and end dates depending on whether they fall on the 31st of a month. European : Always treats the start and end dates as the 30th of the month if they are the 31st. Example Usage: Example 1: Calculate days using the U.S. method =DAYS360("01/01/2024", ...

How to use Microsoft Excel PMT function?

  To use the PMT function in Microsoft Excel, follow these steps: Steps to Use the PMT Function: Open Excel : Open your Excel workbook where you want to calculate the payment. Select a Cell : Click on the cell where you want the result (monthly payment). Enter the PMT Function : Use the formula syntax: =PMT(rate, nper, pv, [fv], [type]) Input Arguments : rate : Interest rate per period (e.g., for a monthly rate, divide the annual rate by 12). nper : Total number of payment periods. pv : Present value (loan amount or investment). fv (optional) : Future value, usually 0 for loans. type (optional) : Payment timing: 0 (default): End of the period. 1: Beginning of the period. Press Enter : Excel will calculate and display the periodic payment. Example 1: Monthly Loan Payment Problem: You take a loan of $15,000 with an annual interest rate of 5%, to be repaid over 3 years (36 months). What is the monthly payment? Steps: Select a cell and enter: =PMT(5%/12, 3*12, -...