bank_with_oops.php
bankaccount.php
<?php
include('db.php');
class bankAccount {
public $balance ;
public $type;
public $source;
public $amount;
public $conn;
public function __construct($type,$source,$amount,$conn){
$this->type=$type;
$this->source = $source;
$this->amount = $amount;
$this->conn = $conn;
}
public function __destruct(){
$this->fetch();
}
public function deposit(){
$conn= $this->conn;
$sql = "INSERT INTO transactions (type, source, amount) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssd", $this->type, $this->source, $this->amount);
// Execute the statement
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
public function withdraw(){
$conn= $this->conn;
$sql = "INSERT INTO transactions (type, source, amount) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssd", $this->type, $this->source, $this->amount);
// Execute the statement
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
public function fetch(){
$conn= $this->conn;
$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 "</tr>";
}
} else {
echo "<tr><td colspan='4'>No transactions found</td></tr>";
}
echo "</table>";
$conn->close();
}
}
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";
?>
execution.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('bankAccount.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$type = $_POST['type'];
$source = $_POST['source'];
$amount = $_POST['amount'];
$bank = new bankAccount($type,$source,$amount,$conn);
if($type =='credit')
{
$bank->deposit();
} else if($type =='debit')
{
$bank->withdraw();
} else {
echo 'none';
}
}
?>
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>
=
No comments:
Post a Comment