File "technician.php"

Full Path: /home/leadltht/fastlinkinternet.com/administrator/data-provider/admin/technician.php
File size: 6.38 KB
MIME-type: text/x-php
Charset: utf-8

<?php
ob_start();  // Start output buffering

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

// Database connection
$conn = new mysqli("server329", "leadltht_prazey1982", "prazey1982123456", "leadltht_fastlinkinternet");

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Define base URL
$base_url = 'https://fastlinkinternet.com/administrator/data-provider/';

// JavaScript Redirect Function
function js_redirect($url) {
    echo "<script type='text/javascript'>window.location.href='{$url}';</script>";
    exit();
}

// Handle Add Technician
if (isset($_POST['add'])) {
    $tech_name = $_POST['tech_name'];

    $sql = "INSERT INTO technician (tech_name) VALUES ('$tech_name')";

    if ($conn->query($sql) === TRUE) {
        js_redirect("{$base_url}admin/?page=technician&success=add");
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

// Handle Edit Technician
if (isset($_POST['edit'])) {
    $id = $_POST['id'];
    $tech_name = $_POST['tech_name'];

    $sql = "UPDATE technician SET tech_name='$tech_name' WHERE id=$id";

    if ($conn->query($sql) === TRUE) {
        js_redirect("{$base_url}admin/?page=technician&success=edit");
    } else {
        echo "Error updating technician: " . $conn->error;
    }
}

// Handle Delete Technician
if (isset($_POST['delete'])) {
    $id = $_POST['id'];

    $sql = "DELETE FROM technician WHERE id=$id";

    if ($conn->query($sql) === TRUE) {
        js_redirect("{$base_url}admin/?page=technician&success=delete");
    } else {
        echo "Error deleting technician: " . $conn->error;
    }
}

ob_end_flush();  // Flush the output buffer and turn off output buffering
?>
<!DOCTYPE html>
<html>
<head>
    <title>Technician Management</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
    <h2 class="mb-4">Manage Technicians</h2>
    
    <?php
    if (isset($_GET['success'])) {
        if ($_GET['success'] == 'add') {
            echo '<div class="alert alert-success">New technician added successfully.</div>';
        } elseif ($_GET['success'] == 'edit') {
            echo '<div class="alert alert-success">Technician updated successfully.</div>';
        } elseif ($_GET['success'] == 'delete') {
            echo '<div class="alert alert-success">Technician deleted successfully.</div>';
        }
    }
    ?>
    
    <div class="card mb-4">
        <div class="card-header">Add Technician</div>
        <div class="card-body">
            <form method="post" action="">
                <div class="form-group">
                    <label for="tech_name">Technician Name:</label>
                    <input type="text" class="form-control" name="tech_name" required>
                </div>
                <input type="submit" name="add" class="btn btn-primary" value="Add Technician">
            </form>
        </div>
    </div>

    <h2 class="mb-4">Technician List</h2>
    <?php
    $sql = "SELECT * FROM technician";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo '<table class="table table-bordered">';
        echo '<thead><tr><th>#</th><th>Technician Name</th><th>Action</th></tr></thead>';
        echo '<tbody>';
        $counter = 1;
        while ($row = $result->fetch_assoc()) {
            echo '<tr>';
            echo '<td>' . $counter . '</td>';
            echo '<td>' . $row["tech_name"] . '</td>';
            echo '<td>
                <a href="javascript:void(0);" class="btn btn-sm btn-warning edit-btn" data-id="' . $row["id"] . '" data-name="' . $row["tech_name"] . '">Edit</a>
                <form method="post" action="" style="display:inline-block;">
                    <input type="hidden" name="id" value="' . $row["id"] . '">
                    <input type="submit" name="delete" class="btn btn-sm btn-danger" value="Delete" onclick="return confirm(\'Are you sure you want to delete this technician?\');">
                </form>
            </td>';
            echo '</tr>';
            $counter++;
        }
        echo '</tbody>';
        echo '</table>';
    } else {
        echo '<p>No technicians found.</p>';
    }

    $conn->close();
    ?>

    <!-- Edit Technician Modal -->
    <div class="modal fade" id="editTechModal" tabindex="-1" role="dialog" aria-labelledby="editTechModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editTechModalLabel">Edit Technician</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form method="post" action="">
                    <div class="modal-body">
                        <input type="hidden" name="id" id="editTechId">
                        <div class="form-group">
                            <label for="editTechName">Technician Name:</label>
                            <input type="text" class="form-control" name="tech_name" id="editTechName" required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="edit" class="btn btn-primary" value="Save Changes">
                    </div>
                </form>
            </div>
        </div>
    </div>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<script>
$(document).ready(function() {
    $('.edit-btn').on('click', function() {
        var id = $(this).data('id');
        var name = $(this).data('name');

        $('#editTechId').val(id);
        $('#editTechName').val(name);

        $('#editTechModal').modal('show');
    });
});
</script>
</body>
</html>