Showing posts with label php_tutorials_from_scratch. Show all posts
Showing posts with label php_tutorials_from_scratch. Show all posts

php tutorials from scratch index

 


https://webdevelopersera.blogspot.com/search/label/%40StudentTechMitra?updated-max=2024-05-08T18:36:00-07:00&max-results=20&start=28&by-date=false


https://webdevelopersera.blogspot.com/2024/05/advanced-php-tutoirials-from-scratch.html


https://webdevelopersera.blogspot.com/2024/05/advanced-php-tutoirials-from-scratch_8.html


https://webdevelopersera.blogspot.com/2024/05/blog-post.html

php tutorial -> develop bank applicaiton with oops concept

 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
}
}
}
?>

=

php tutorial -> develop bank appliaiton with oops

 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>

=

php tutorial -> bank applicaiton with direct query

 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();
}
?>

=


AI Tools

 Midjourney oter.ai aiva googlegemin dall-e copilot jasper copilot openaiplayground