BIT HELPER UOM

ads header

Search This Blog

Monday 10 June 2024

Assignment 2 PHP Answer | source code |UOM

 admin.php


<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "WaterBills";


$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}


if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $id = $_POST['id'];

    $range_start = $_POST['range_start'];

    $range_end = $_POST['range_end'];

    $energy_charge = $_POST['energy_charge'];

    $fixed_charge = $_POST['fixed_charge'];


    $sql = "UPDATE water_bill_units SET range_start='$range_start', range_end='$range_end', energy_charge='$energy_charge', fixed_charge='$fixed_charge' WHERE id='$id'";


    if ($conn->query($sql) === TRUE) {

      echo "<div class='alert alert-success'>Record updated successfully</div>";

    } else {

      echo "<div class='alert alert-danger'>Error: " . $sql . "<br>" . $conn->error . "</div>";

    }

}


$result = $conn->query("SELECT * FROM water_bill_unit");


$conn->close();

?>


<!DOCTYPE html>

<html>

<head>

    <title>Admin Panel</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>

        body {

            background-color: #f8f9fa;

            padding-top: 20px;

        }

        .table th, .table td {

            vertical-align: middle;

            text-align: center;

        }

        .form-control {

            display: inline-block;

            width: auto;

        }

        .btn-primary {

            margin-top: 10px;

        }

        .card {

            margin-top: 20px;

            margin-bottom: 20px;

        }

    </style>

</head>

<body>


<div class="container">

    <h2 class="text-center">Admin Panel</h2>


    <table class="table table-sm table-bordered table-hover mt-4">

        <thead class="thead-light">

            <tr>

                <th>ID</th>

                <th>Range Start</th>

                <th>Range End</th>

                <th>Energy Charge</th>

                <th>Fixed Charge</th>

                <th>Action</th>

            </tr>

        </thead>

        <tbody>

        <?php while($row = $result->fetch_assoc()) { ?>

            <tr>

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

                <td><?php echo $row['range_start']; ?></td>

                <td><?php echo $row['range_end']; ?></td>

                <td><?php echo $row['energy_charge']; ?></td>

                <td><?php echo $row['fixed_charge']; ?></td>

                <td>

                    <form method="post" class="form-inline">

                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">

                        <input type="text" name="range_start" class="form-control mb-2 mr-sm-2" value="<?php echo $row['range_start']; ?>">

                        <input type="text" name="range_end" class="form-control mb-2 mr-sm-2" value="<?php echo $row['range_end']; ?>">

                        <input type="text" name="energy_charge" class="form-control mb-2 mr-sm-2" value="<?php echo $row['energy_charge']; ?>">

                        <input type="text" name="fixed_charge" class="form-control mb-2 mr-sm-2" value="<?php echo $row['fixed_charge']; ?>">

                        <input type="submit" class="btn btn-primary mb-2" value="Update">

                    </form>

                </td>

            </tr>

        <?php } ?>

        </tbody>

    </table>


    <div class="card mx-auto w-50">

        <div class="card-body">

            <h5 class="card-title">New Admin Registration</h5>

            <p class="card-text">Register a new admin for the system.</p>

            <center>

                <button class="btn btn-primary" onclick="location.href='adminregistration.php'">Register</button>

            </center>

        </div>

    </div>

</div>


</body>

</html>





adminlogin.php



<!DOCTYPE html>

<html>

<head>

    <title>ADMIN LOGIN</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>

        body {

            background-color: #f8f9fa;

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

            margin: 0;

        }

        .login-container {

            background-color: white;

            padding: 30px;

            border-radius: 10px;

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

            width: 100%;

            max-width: 400px;

        }

        h2 {

            margin-bottom: 20px;

        }

        .input-group {

            margin-bottom: 15px;

        }

        .input-group label {

            width: 100%;

        }

        .input-group input {

            width: 100%;

        }

        button {

            width: 100%;

        }

    </style>

</head>

<body>


<div class="login-container">

    <h2 class="text-center">Login</h2>

    <form method="post">

        <div class="input-group">

            <label for="username">Username:</label>

            <input type="text" name="username" class="form-control" required>

        </div>

        <div class="input-group">

            <label for="password">Password:</label>

            <input type="password" name="password" class="form-control" required>

        </div>

        <button type="submit" class="btn btn-primary">Login</button>

    </form> 

</div>


<div>

    <?php

    session_start();


    // Database configuration

    $servername = "localhost";

    $username = "root"; // Replace with your MySQL username

    $password = ""; // Replace with your MySQL password

    $dbname = "WaterBills"; // this is your database name


    // Create connection

    $conn = new mysqli($servername, $username, $password, $dbname);


    // Check connection

    if ($conn->connect_error) {

        die("Connection failed: " . $conn->connect_error);

    }


    // Function to check login credentials

    function check_login($conn, $username, $password) {

        $stmt = $conn->prepare("SELECT password FROM admin WHERE username = ?");

        $stmt->bind_param("s", $username);

        $stmt->execute();

        $stmt->store_result();


        $hashed_password = $password;

        

        if ($stmt->num_rows > 0) {

            $stmt->bind_result($hashed_password);

            $stmt->fetch();

            if (password_verify($password, $hashed_password)) {

                return true;

            }

        }

        return false;

    }


    // Handle login form submission

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $username = $_POST['username'];

        $password = $_POST['password'];

        // $password = password_hash($_POST['password'], PASSWORD_BCRYPT);

        

        if (check_login($conn, $username, $password)) {

            $_SESSION['username'] = $username;

            header("Location: admin.php");

            exit();

        } else {

            echo "<div class='alert alert-danger mt-3'>Invalid username or password.</div>";

        }

    }


    $conn->close();

    ?>

</div>


</body>

</html>



adminregistration.php



<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "WaterBills";


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}


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

    $username = $_POST['username'];

    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);


    $sql = "INSERT INTO admin (username, password) VALUES ('$username', '$password')";


    if ($conn->query($sql) === TRUE) {

        header("Location: adminlogin.php");

    } else {

      echo "Error: " . $sql . "<br>" . $conn->error;

    }

}


$conn->close();

?>


<!DOCTYPE html>

<html>

<head>

    <title>Admin Register Form</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>

        body {

            background-color: #f8f9fa;

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

            margin: 0;

        }

        .register-container {

            background-color: white;

            padding: 30px;

            border-radius: 10px;

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

            width: 100%;

            max-width: 500px;

        }

        h2 {

            margin-bottom: 20px;

        }

    </style>

</head>

<body>


<div class="register-container">

    <h2 class="text-center">Admin Register</h2>

    <hr>

    <form action="" method="post">

        <div class="form-group row">

            <label for="username" class="col-sm-3 col-form-label"><b>Username:</b></label>

            <div class="col-sm-9">

                <input type="text" class="form-control" name="username" required>

            </div>

        </div>

        <div class="form-group row">

            <label for="password" class="col-sm-3 col-form-label"><b>Password:</b></label>

            <div class="col-sm-9">

                <input type="password" class="form-control" name="password" required>

            </div>

        </div>

        <button type="submit" class="btn btn-primary btn-block" name="submit">Register</button>

    </form>

</div>


</body>

</html>



dashboard.php


<!DOCTYPE html>

<html>

<head>

    <title>Dashboard</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>

        body {

            background-color: #f8f9fa;

            padding-top: 50px;

        }

        .container {

            max-width: 600px;

            background-color: white;

            padding: 30px;

            border-radius: 10px;

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

        }

        h1 {

            margin-bottom: 20px;

        }

        footer {

            margin-top: 30px;

        }

        footer .card {

            margin: 10px 0;

            padding: 10px;

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

        }

        .btn-primary {

            width: 100%;

        }

    </style>

</head>

<body>

<div class="container">

    <h1>Enter Water Usage</h1>

    <form method="post">

        <div class="form-group">

            <label for="month">Month:</label>

            <input type="month" class="form-control" name="month" required>

        </div>

        <div class="form-group">

            <label for="units">Units:</label>

            <input type="number" class="form-control" name="units" required>

        </div>

        <button type="submit" class="btn btn-primary" name="submit">Submit</button>

    </form>

</div>


<div class="container mt-5">

    <?php

    $servername = "localhost";

    $username = "root";

    $password = "";

    $dbname = "WaterBills";


    // Create connection

    $conn = new mysqli($servername, $username, $password, $dbname);


    // Check connection

    if ($conn->connect_error) {

        die("Connection failed: " . $conn->connect_error);

    }


    session_start();

    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {

        $customer_id = $_SESSION['customer_id'];

        $month = $_POST['month'];

        $units = $_POST['units'];


        // Calculate Bill

        $sql = "SELECT * FROM water_bill_unit ORDER BY range_start ASC";

        $result = $conn->query($sql);


        $total_energy_charge = 0;

        $fixed_charge = 0;

        $remaining_units = $units;


        while ($row = $result->fetch_assoc()) {

            if ($row['range_end'] === NULL || $remaining_units <= ($row['range_end'] - $row['range_start'] + 1)) {

                $total_energy_charge += $remaining_units * $row['energy_charge'];

                $fixed_charge = $row['fixed_charge'];

                break;

            } else {

                $units_in_range = $row['range_end'] - $row['range_start'] + 1;

                $total_energy_charge += $units_in_range * $row['energy_charge'];

                $fixed_charge = $row['fixed_charge'];

                $remaining_units -= $units_in_range;

            }

        }


        $total_charge = $total_energy_charge + $fixed_charge;

        $vat = $total_charge * 0.18;

        $final_bill = $total_charge + $vat;


        // Insert water usage and total charge into database

        $sql = "INSERT INTO water_usage (customer_id, month, units, total) VALUES ('$customer_id', '$month', '$units', '$final_bill')";

        if ($conn->query($sql) === TRUE) {

            echo "<div class='alert alert-success'>Usage and bill recorded successfully</div>";

        } else {

            echo "<div class='alert alert-danger'>Error: " . $sql . "<br>" . $conn->error . "</div>";

        }


        echo "<div class='alert alert-info'>";

        echo "Total Charge: " . $total_charge . " LKR<br>";

        echo "VAT (18%): " . $vat . " LKR<br>";

        echo "Final Bill: " . $final_bill . " LKR<br>";

        echo "</div>";

    }


    $conn->close();

    ?>

</div>


<footer>

    <div class="container">

        <div class="card text-center">

            <p><b>Water Usage History:</b></p>

            <button class="btn btn-secondary" onclick="location.href='history.php'">History</button>

        </div>

        <div class="card text-center">

            <form method="get" action="logout.php">

                <button class="btn btn-danger" type="submit">Logout</button>

            </form>

        </div>

    </div>

</footer>


<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</body>

</html>



history.php



<?php


$servername = "localhost";

$username = "root";

$password = "";

$dbname = "WaterBills";


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}


session_start();

if (!isset($_SESSION['customer_id'])) {

    header("Location: login.php");

    exit();

}


$customer_id = $_SESSION['customer_id'];

$sql = "SELECT * FROM water_usage WHERE customer_id = ?";

$stmt = $conn->prepare($sql);

$stmt->bind_param("i", $customer_id);

$stmt->execute();

$result = $stmt->get_result();

?>


<!DOCTYPE html>

<html>

<head>

    <title>Usage History</title>

    <!-- Bootstrap CSS -->

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->

    <style>

        body {

            background-color: #f8f9fa;

            margin: 20px;

        }

        h1 {

            margin-bottom: 20px;

        }

        table {

            width: 80%;

            margin: 0 auto;

            border-collapse: collapse;

        }

        th, td {

            padding: 10px;

            text-align: center;

            border: 1px solid #dee2e6;

        }

        th {

            background-color: #007bff;

            color: white;

        }

        tr:nth-child(even) {

            background-color: #f2f2f2;

        }

        .btn-secondary {

            margin-top: 20px;

        }

    </style>

</head>

<body>

    <div class="container">

        <h1 class="text-center">Your Water Usage History</h1>

        <hr>

        <center>

        <table class="table table-striped table-bordered">

            <thead class="thead-dark">

                <tr>

                    <th>Month</th>

                    <th>Units Used</th>

                    <th>Amount</th>

                </tr>

            </thead>

            <tbody>

                <?php while ($row = $result->fetch_assoc()) { ?>

                    <tr>

                        <td><?php echo htmlspecialchars($row['month']); ?></td>

                        <td><?php echo htmlspecialchars($row['units']); ?></td>

                        <td><?php echo htmlspecialchars($row['total']); ?></td>

                    </tr>

                <?php } ?>

            </tbody>

        </table>

        <form method="get" action="dashboard.php">

            <button class="btn btn-secondary" type="submit">Back</button>

        </form>

        </center>

    </div>

</body>

</html>


<?php

$stmt->close();

$conn->close();

?>



login.php


<!DOCTYPE html>

<html>

<head>

    <title>LOGIN</title>

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>

        body {

            background-color: #f8f9fa;

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

        }

        .login-container {

            background-color: white;

            padding: 30px;

            border-radius: 10px;

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

            width: 300px;

        }

        h2 {

            text-align: center;

            margin-bottom: 20px;

        }

        .input-group {

            margin-bottom: 15px;

        }

        .input-group label {

            width: 100%;

            margin-bottom: 5px;

        }

        .btn {

            width: 100%;

        }

    </style>

</head>

<body>

<div class="login-container">

    <h2>Login</h2>

    <form method="post">

        <div class="input-group">

            <label for="username">Username:</label>

            <input type="text" class="form-control" name="username" required>

        </div>

        <div class="input-group">

            <label for="password">Password:</label>

            <input type="password" class="form-control" name="password" required>

        </div>

        <button type="submit" class="btn btn-primary">Login</button>

    </form>

    <div>

        <?php

        $servername = "localhost";

        $username = "root";

        $password = "";

        $dbname = "WaterBills";


        $conn = new mysqli($servername, $username, $password, $dbname);


        if ($conn->connect_error) {

            die("Connection failed: " . $conn->connect_error);

        }


        session_start();


        if ($_SERVER["REQUEST_METHOD"] == "POST") {

            $username = $_POST['username'];

            $password = $_POST['password'];


            $sql = "SELECT * FROM customers WHERE username='$username'";

            $result = $conn->query($sql);


            if ($result->num_rows > 0) {

                $row = $result->fetch_assoc();

                if (password_verify($password, $row['password'])) {

                    $_SESSION['customer_id'] = $row['id'];

                    header("Location: dashboard.php");

                } else {

                    echo '<br><div class="alert alert-danger" role="alert">Invalid password</div>';

                }

            } else {

                echo '<br><div class="alert alert-danger" role="alert">No user found with that username</div>';

            }

        }


        $conn->close();

        ?>

    </div>

</div>


<!-- Bootstrap JS and dependencies -->

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</body>

</html>



logindashboard.php


<!DOCTYPE html>

<html>

<head>

    <title>Login Dashboard</title>

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>

        body {

            background-color: #f8f9fa;

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

        }

        .row {

            width: 80%;

        }

        .card {

            margin-bottom: 20px;

        }

        .card-body {

            text-align: center;

        }

        #center_button button {

            width: 100%;

        }

    </style>

</head>

<body>

<div class="row">

  <div class="col-md-6">

    <div class="card">

      <div class="card-body">

        <h5 class="card-title">Admin Panel</h5>

        <p class="card-text">Admin only Access - go to login page</p>

        <div id="center_button"><button class="btn btn-primary" onclick="location.href='adminlogin.php'">Admin</button></div>

      </div>

    </div>

  </div>

  <div class="col-md-6">

    <div class="card">

      <div class="card-body">

        <h5 class="card-title">Customer Registration</h5>

        <p class="card-text">New Registration and login page</p>

        <div id="center_button"><button class="btn btn-success" onclick="location.href='registration.php'">Register</button></div><br>

        <p class="card-text">Login page</p>

        <div id="center_button"><button class="btn btn-secondary" onclick="location.href='login.php'">Login</button></div>

      </div>

    </div>

  </div>

</div>


<!-- Bootstrap JS and dependencies -->

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</body>

</html>



logout.php


<?php

session_start();

session_unset();

session_destroy();

header("Location: login.php");

exit();

?>



registration.php


<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "WaterBills";


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}


// if ($_SERVER["REQUEST_METHOD"] == "POST") 

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

    $name = $_POST['name'];

    $address = $_POST['address'];

    $email = $_POST['email'];

    $water_bill_number = $_POST['water_bill_number'];

    $username = $_POST['username'];

    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);


    $sql = "INSERT INTO customers (name, address, email, water_bill_number, username, password)

    VALUES ('$name', '$address', '$email', '$water_bill_number', '$username', '$password')";


    if ($conn->query($sql) === TRUE) {

        header("Location: login.php");

      // echo "New record created successfully";

      //echo '<script> location.replace("login.php")</script>';  


    } else {

      echo "Error: " . $sql . "<br>" . $conn->error;

    }

}


$conn->close();

?>


<!DOCTYPE html>

<html>

<head>

    <title>Register Form</title>

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>

        body {

            background-color: #f8f9fa;

        }

        .form-container {

            margin-top: 50px;

            padding: 20px;

            background-color: #fff;

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

            border-radius: 5px;

        }

    </style>

</head>

<body>


<div class="container">

    <div class="row justify-content-center">

        <div class="col-md-8">

            <div class="form-container">

                <h2 class="text-center mt-3">Register</h2>

                <hr>


                <form action="" method="post">

                    <div class="form-group">

                        <label for="name"><b>Name:</b></label>

                        <input type="text" name="name" required class="form-control">

                    </div>


                    <div class="form-group">

                        <label for="address"><b>Address:</b></label>

                        <input type="text" name="address" required class="form-control border-danger">

                    </div>


                    <div class="form-group">

                        <label for="email"><b>Email:</b></label>

                        <input type="email" name="email" required class="form-control border-danger">

                    </div>


                    <div class="form-group">

                        <label for="water_bill_number"><b>Water Bill Number:</b></label>

                        <input type="text" name="water_bill_number" required class="form-control border-danger">

                    </div>


                    <div class="form-group">

                        <label for="username"><b>Username:</b></label>

                        <input type="text" name="username" required class="form-control border-danger">

                    </div>


                    <div class="form-group">

                        <label for="password"><b>Password:</b></label>

                        <input type="password" name="password" required class="form-control border-danger">

                    </div>


                    <button type="submit" name="submit" class="btn btn-primary">Register</button>

                    <br><br>

                    <div id="center_button">

                        <button type="button" class="btn btn-secondary" onclick="location.href='login.php'">Login</button>

                    </div>

                </form>

            </div>

        </div>

    </div>

</div>


<!-- Bootstrap JS and dependencies -->

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</body>

</html>


No comments:

Post a Comment

Powered by Blogger.

Assignment 2 PHP Answer | source code |UOM

 admin.php <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "WaterBills&...

Sponsor

Ahamed Hather

Popular Posts