Source Code

				
					----storing the form-----
<?php
// Include WordPress configuration
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $name = sanitize_text_field($_POST['name']);
    $age = intval($_POST['age']); // Convert to integer
    $degree = sanitize_text_field($_POST['degree']);

    // Insert data into the database
    global $wpdb;
    $table_name = 'user_details_crud';
    $result = $wpdb->insert(
        $table_name,
        array(
            'name' => $name,
            'age' => $age,
            'degree' => $degree,
        ),
        array('%s', '%d', '%s')
    );

    // Check for errors
    if ($result === false) {
        echo $wpdb->last_error;
    } else {
        // Return a success response
        echo 'success';
        exit;
    }
}
?>
				
			
				
					----------get the form-------
<!DOCTYPE html>
<html lang="en">
<head>
    <title>CRUD projects</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="font-family: Arial, sans-serif; margin: 20px; padding: 0; background-color: #f2f2f2;">

    <h2 style="color: #333; text-align: center;">Submit Form</h2>

    <form id="submit-form" action="/wp-content/themes/woodmart/crud_project.php" method="post" style="max-width: 400px; margin: 0 auto; background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);">

        <label for="name" style="display: block; margin-bottom: 8px; font-weight: bold;">Name:</label>
        <input type="text" id="name" name="name" required style="width: 100%; padding: 8px; margin-bottom: 16px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px;">

        <label for="age" style="display: block; margin-bottom: 8px; font-weight: bold;">Age:</label>
        <input type="number" id="age" name="age" required style="width: 100%; padding: 8px; margin-bottom: 16px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px;">

        <label for="degree" style="display: block; margin-bottom: 8px; font-weight: bold;">Degree:</label>
        <select id="degree" name="degree" required style="width: 100%; padding: 8px; margin-bottom: 16px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px;">
            <option value="Engineering">Engineering</option>
            <option value="Computer Science">Computer Science</option>
            <option value="Business Administration">Business Administration</option>
            <option value="Finance">Finance</option>
            <option value="Marketing">Marketing</option>
            <option value="Psychology">Psychology</option>
            <option value="Biology">Biology</option>
            <option value="Chemistry">Chemistry</option>
            <option value="Physics">Physics</option>
            <option value="Mathematics">Mathematics</option>
        </select>
        
        <button type="submit" style="background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; display: block; width: 100%; font-size: 16px;">Submit</button>
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
        // Handle form submission
        $('#submit-form').submit(function(e) {
            e.preventDefault(); // Prevent default form submission

            // Submit form data asynchronously
            $.ajax({
                type: 'POST',
                url: '/wp-content/themes/woodmart/crud_project.php', // Path to your PHP script
                data: $('#submit-form').serialize(), // Serialize form data
                success: function(response) {
                    if (response === 'success') {
                        // If form submission is successful, redirect to specified URL
                        window.location.href = 'https://www.aourz.com/crud3/';
                    } else {
                        alert('Error: ' + response); // Display error message
                    }
                },
                error: function(xhr, status, error) {
                    console.error('Error: ' + error); // Log error to console
                }
            });
        });
    });
    </script>
    <br>
    <hr style="border: 0; height: 1px; background-color: #ccc; margin-top: 20px;">

</body>
</html>

				
			
				
					<form method='get'><input type='text' name='search' placeholder='Search...' value=''><button type='submit'>Search</button></form><form method='post'><table border='1'><tr><th>Select</th><th><a href='?sort_column=id&sort_order=ASC'>ID</a></th><th><a href='?sort_column=name&sort_order=ASC'>Name</a></th><th><a href='?sort_column=age&sort_order=ASC'>Age</a></th><th><a href='?sort_column=degree&sort_order=ASC'>Degree</a></th><th><a href='?sort_column=entry_time&sort_order=ASC'>Entry Time</a></th><th>Action</th></tr><tr><td><input type='checkbox' name='delete_ids[]' value='75'></td><td>75</td><td>Sivasaran saravanan</td><td>23</td><td>Engineering</td><td>2024-08-10 10:43:37</td><td><button type='button' onclick='showUpdateForm(75, "Sivasaran saravanan", 23, "Engineering")'>Edit</button></td></tr><tr><td><input type='checkbox' name='delete_ids[]' value='45'></td><td>45</td><td>NAVEENMANI S</td><td>21</td><td>Engineering</td><td>2024-02-04 20:14:29</td><td><button type='button' onclick='showUpdateForm(45, "NAVEENMANI S", 21, "Engineering")'>Edit</button></td></tr><tr><td><input type='checkbox' name='delete_ids[]' value='1'></td><td>1</td><td>John Dow</td><td>30</td><td>Computer Science</td><td>2024-02-02 23:34:31</td><td><button type='button' onclick='showUpdateForm(1, "John Dow", 30, "Computer Science")'>Edit</button></td></tr></table><button type='submit' name='delete_records'>Delete Selected Records</button></form><script>
        function showUpdateForm(id, name, age, degree) {
            document.getElementById('update-form').style.display = 'block';
            document.getElementById('update_id').value = id;
            document.getElementById('update_name').value = name;
            document.getElementById('update_age').value = age;
            document.getElementById('update_degree').value = degree;
        }
    </script><div id='update-form' style='display: none;'><h2>Update Record</h2><form method='post'><input type='hidden' name='update_id' id='update_id' value=''><label for='update_name'>Name:</label><input type='text' id='update_name' name='update_name' required><label for='update_age'>Age:</label><input type='number' id='update_age' name='update_age' required><label for='update_degree'>Degree:</label><select id='update_degree' name='update_degree' required><option value='Engineering'>Engineering</option><option value='Computer Science'>Computer Science</option><option value='Business Administration'>Business Administration</option><option value='Finance'>Finance</option><option value='Marketing'>Marketing</option><option value='Psychology'>Psychology</option><option value='Biology'>Biology</option><option value='Chemistry'>Chemistry</option><option value='Physics'>Physics</option><option value='Mathematics'>Mathematics</option></select><button type='submit' name='update_records'>Update Record</button></form></div>
CRUD TYPE PROJECT
-----------------------------------
<?php
// Include WordPress configuration
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Function to display records, handle sorting, deletions, and updates
function displayRecords() {
    global $wpdb;

    // Check if delete button is clicked
    if (isset($_POST['delete_records']) && !empty($_POST['delete_ids'])) {
        // Retrieve selected IDs to delete
        $delete_ids = $_POST['delete_ids'];

        // Construct the delete query
        $delete_query = "DELETE FROM user_details_crud WHERE id IN (" . implode(',', $delete_ids) . ")";

        // Execute the delete query
        $wpdb->query($delete_query);

        // Redirect to the same page to refresh the records
        echo "<meta http-equiv='refresh' content='0'>";
    }

    // Check if update button is clicked
    if (isset($_POST['update_records'])) {
        // Retrieve form data for update
        $update_id = intval($_POST['update_id']); // Convert to integer
        $update_name = sanitize_text_field($_POST['update_name']);
        $update_age = intval($_POST['update_age']); // Convert to integer
        $update_degree = sanitize_text_field($_POST['update_degree']);

        // Update data in the database
        $table_name = 'user_details_crud';
        $wpdb->update(
            $table_name,
            array(
                'name' => $update_name,
                'age' => $update_age,
                'degree' => $update_degree,
            ),
            array('id' => $update_id),
            array('%s', '%d', '%s'),
            array('%d')
        );

        // Redirect to the same page to refresh the records
        echo "<meta http-equiv='refresh' content='0'>";
    }

    // Set default sorting column and direction
    $sort_column = isset($_GET['sort_column']) ? $_GET['sort_column'] : 'id';
    $sort_order = isset($_GET['sort_order']) ? $_GET['sort_order'] : 'DESC';

    // Set default search term
    $search_term = isset($_GET['search']) ? $_GET['search'] : '';

    // Construct the SQL query
    $query = "SELECT * FROM user_details_crud";

    // Add search functionality
    if (!empty($search_term)) {
        $query .= " WHERE name LIKE '%{$search_term}%' OR age LIKE '%{$search_term}%' OR degree LIKE '%{$search_term}%' OR entry_time LIKE '%{$search_term}%'";
    }

    // Add sorting functionality
    $query .= " ORDER BY $sort_column $sort_order";

    // Retrieve records from the database
    $results = $wpdb->get_results($query);

    // Display the records in a table with delete checkboxes, sorting links, and update forms
    echo "<form method='get'>";
    echo "<input type='text' name='search' placeholder='Search...' value='" . $search_term . "'><button type='submit'>Search</button>";
    echo "</form>";
    echo "<form method='post'>";
    echo "<table border='1'>";
    echo "<tr><th>Select</th><th><a href='?sort_column=id&sort_order=" . ($sort_column == 'id' && $sort_order == 'ASC' ? 'DESC' : 'ASC') . "'>ID</a></th><th><a href='?sort_column=name&sort_order=" . ($sort_column == 'name' && $sort_order == 'ASC' ? 'DESC' : 'ASC') . "'>Name</a></th><th><a href='?sort_column=age&sort_order=" . ($sort_column == 'age' && $sort_order == 'ASC' ? 'DESC' : 'ASC') . "'>Age</a></th><th><a href='?sort_column=degree&sort_order=" . ($sort_column == 'degree' && $sort_order == 'ASC' ? 'DESC' : 'ASC') . "'>Degree</a></th><th><a href='?sort_column=entry_time&sort_order=" . ($sort_column == 'entry_time' && $sort_order == 'ASC' ? 'DESC' : 'ASC') . "'>Entry Time</a></th><th>Action</th></tr>";

    foreach ($results as $row) {
        echo "<tr>";
        echo "<td><input type='checkbox' name='delete_ids[]' value='" . $row->id . "'></td>";
        echo "<td>" . $row->id . "</td>";
        echo "<td>" . $row->name . "</td>";
        echo "<td>" . $row->age . "</td>";
        echo "<td>" . $row->degree . "</td>";
        echo "<td>" . $row->entry_time . "</td>";
        // Add Edit button with a form to update the record
        echo "<td><button type='button' onclick='showUpdateForm(" . $row->id . ", \"" . $row->name . "\", " . $row->age . ", \"" . $row->degree . "\")'>Edit</button></td>";
        echo "</tr>";
    }

    echo "</table>";
    echo "<button type='submit' name='delete_records'>Delete Selected Records</button>";
    echo "</form>";

    // JavaScript to handle the update form visibility
    echo "<script>
        function showUpdateForm(id, name, age, degree) {
            document.getElementById('update-form').style.display = 'block';
            document.getElementById('update_id').value = id;
            document.getElementById('update_name').value = name;
            document.getElementById('update_age').value = age;
            document.getElementById('update_degree').value = degree;
        }
    </script>";

    // Update form
    echo "<div id='update-form' style='display: none;'>";
    echo "<h2>Update Record</h2>";
    echo "<form method='post'>";
    echo "<input type='hidden' name='update_id' id='update_id' value=''>";
    echo "<label for='update_name'>Name:</label>";
    echo "<input type='text' id='update_name' name='update_name' required>";
    echo "<label for='update_age'>Age:</label>";
    echo "<input type='number' id='update_age' name='update_age' required>";
    echo "<label for='update_degree'>Degree:</label>";
    echo "<select id='update_degree' name='update_degree' required>";
    echo "<option value='Engineering'>Engineering</option>";
    echo "<option value='Computer Science'>Computer Science</option>";
    echo "<option value='Business Administration'>Business Administration</option>";
    echo "<option value='Finance'>Finance</option>";
    echo "<option value='Marketing'>Marketing</option>";
    echo "<option value='Psychology'>Psychology</option>";
    echo "<option value='Biology'>Biology</option>";
    echo "<option value='Chemistry'>Chemistry</option>";
    echo "<option value='Physics'>Physics</option>";
    echo "<option value='Mathematics'>Mathematics</option>";
    echo "</select>";
    echo "<button type='submit' name='update_records'>Update Record</button>";
    echo "</form>";
    echo "</div>";
}

// Call the function to display records, handle sorting, deletions, and updates
displayRecords();
?>