bank_with_oops_o
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transaction Form</title>
</head>
<body>
<h2>Transaction Form</h2>
<form action="execution.php" method="post">
<label for="type">Type:</label>
<select name="type" id="type">
<option value="credit">Credit</option>
<option value="debit">Debit</option>
</select><br><br>
<label for="source">Source:</label>
<input type="text" name="source" id="source"><br><br>
<label for="amount">Amount:</label>
<input type="number" name="amount" id="amount" step="0.01"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
execution.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('db.php'); // Include the database connection
include('bank_account.php'); // Include the BankAccount class
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$accountNumber = 11; // You might need to get the account number dynamically
$type = $_POST['type'];
$source = $_POST['source'];
$amount = $_POST['amount'];
// Create a new BankAccount object and pass the database connection
$account = new BankAccount($accountNumber, $conn);
// Execute transaction based on type
if ($type == 'credit') {
$account->deposit($amount);
} elseif ($type == 'debit') {
$account->withdraw($amount);
} else {
echo "Invalid transaction type";
}
}
?>
db.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbName);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";
?>
fetch.php
<?php
// fetch.php
require('db.php');
include('transaction.php');
// Fetch data from transactions table
$sql = "SELECT * FROM transactions";
$result = $conn->query($sql);
echo "<table>";
// Check if there are any rows returned
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "<td>" . $row["source"] . "</td>";
echo "<td>" . $row["amount"] . "</td>";
echo "<td>" . $row["created_at"] . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='5'>No transactions found</td></tr>";
}
echo "</table>";
$conn->close();
?>
bank_Account.php
<?php
include('db.php');
class BankAccount {
private $accountNumber;
private $balance;
private $conn; // Add a property to store the database connection
public function __construct($accountNumber, $conn) {
$this->accountNumber = $accountNumber;
$this->conn = $conn; // Store the database connection
// Initialize balance to 0
$this->balance = 0;
}
public function __destruct()
{
$this->getBalance();
}
public function deposit($amount) {
$conn = $this->conn; // Use the stored database connection
// Prepare and bind the SQL statement
$sql = "INSERT INTO transactions (type, source, amount, created_at) VALUES (?, ?, ?, NOW())";
$type = 'credit';
$source = 'deposit';
$timestamp = date("Y-m-d H:i:s");
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssd", $type, $source, $amount);
// Execute the statement
if ($stmt->execute()) {
echo "Deposit recorded successfully </br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close statement
$stmt->close();
}
public function withdraw($amount) {
$conn = $this->conn; // Use the stored database connection
// Ensure balance is up to date
$this->getBalance();
if ($this->balance >= $amount) {
// Prepare and bind the SQL statement
$sql = "INSERT INTO transactions (type, source, amount, created_at) VALUES (?, ?, ?, NOW())";
$type = 'debit';
$source = 'withdrawal';
$timestamp = date("Y-m-d H:i:s");
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssd", $type, $source, $amount);
// Execute the statement
if ($stmt->execute()) {
echo "Withdrawal recorded successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close statement
$stmt->close();
} else {
echo "Insufficient balance";
}
}
public function getBalance() {
$conn = $this->conn; // Use the stored database connection
// Prepare and bind the SQL statement to fetch all transactions
$sql = "SELECT SUM(CASE WHEN type='credit' THEN amount ELSE -amount END) AS balance FROM transactions";
$stmt = $conn->prepare($sql);
// Execute the statement
$stmt->execute();
// Get the result
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$this->balance = $row['balance'];
echo $row['balance'];
} else {
return 0; // No transactions found
}
}
}
?>
=