bank
create_table.php
<?php
include('db.php');
$sql = "CREATE TABLE transactions (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
type ENUM('credit', 'debit') NOT NULL,
amount DECIMAL(10,2) NOT NULL,
source VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "TABLE created successfully";
} else {
echo "Error creating TABLE: " . $conn->error;
}
$conn->close();
?>
create_Transaction.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="insert_transaction.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>
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';
?>
fetch.php
<?php
require('db.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 "</tr>";
}
} else {
echo "<tr><td colspan='4'>No transactions found</td></tr>";
}
echo "</table>";
$conn->close();
?>
insert_Transaction.php
<?php
include('db.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$type = $_POST['type'];
$source = $_POST['source'];
$amount = $_POST['amount'];
// Prepare and bind the SQL statement
$sql = "INSERT INTO transactions (type, source, amount) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssd", $type, $source, $amount);
// Execute the statement
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close statement and connection
$stmt->close();
$conn->close();
}
?>
=
No comments:
Post a Comment