0% found this document useful (0 votes)
19 views4 pages

investment platform

Uploaded by

selinasacc190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

investment platform

Uploaded by

selinasacc190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE DATABASE investment_platform;

USE investment_platform;

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);

CREATE TABLE accounts (


id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
account_type ENUM('retirement', 'stocks', 'mutual_funds') NOT NULL,
balance DECIMAL(10, 2) DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE transactions (


id INT AUTO_INCREMENT PRIMARY KEY,
account_id INT,
transaction_type ENUM('deposit', 'withdrawal') NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (account_id) REFERENCES accounts(id)
);

register.php-
<?php
$conn = new mysqli("localhost", "root", "", "investment_platform");

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


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

$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");


$stmt->bind_param("ss", $username, $password);

if ($stmt->execute()) {
echo "Registration successful.";
} else {
echo "Error: Could not register user.";
}
}
?>

<h2>Register</h2>
<form method="post">
<label>Username: <input type="text" name="username" required></label><br>
<label>Password: <input type="password" name="password" required></label><br>
<button type="submit">Register</button>
</form>

login.php-
<?php
session_start();
$conn = new mysqli("localhost", "root", "", "investment_platform");

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


$username = $_POST['username'];
$password = $_POST['password'];

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


$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user_id, $hashed_password);
$stmt->fetch();

if ($stmt->num_rows > 0 && password_verify($password, $hashed_password)) {


$_SESSION['user_id'] = $user_id;
header("Location: dashboard.php");
} else {
echo "Invalid username or password.";
}
}
?>

<h2>Login</h2>
<form method="post">
<label>Username: <input type="text" name="username" required></label><br>
<label>Password: <input type="password" name="password" required></label><br>
<button type="submit">Login</button>
</form>

dashboard.php-
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}

$conn = new mysqli("localhost", "root", "", "investment_platform");


$user_id = $_SESSION['user_id'];

// Fetch user accounts


$accounts = $conn->query("SELECT * FROM accounts WHERE user_id = $user_id");
?>

<h2>Your Accounts</h2>
<form method="post" action="transaction.php">
<label>Select Account:
<select name="account_id">
<?php while ($account = $accounts->fetch_assoc()): ?>
<option value="<?= $account['id'] ?>"><?=
ucfirst($account['account_type']) ?> (Balance: $<?= $account['balance']
?>)</option>
<?php endwhile; ?>
</select>
</label><br>
<label>Transaction Type:
<select name="transaction_type">
<option value="deposit">Deposit</option>
<option value="withdrawal">Withdrawal</option>
</select>
</label><br>
<label>Amount: <input type="number" name="amount" step="0.01" min="0.01"
required></label><br>
<button type="submit">Submit Transaction</button>
</form>

<a href="transaction_history.php">View Transaction History</a>

transaction.php-
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}

$conn = new mysqli("localhost", "root", "", "investment_platform");

$account_id = $_POST['account_id'];
$transaction_type = $_POST['transaction_type'];
$amount = (float) $_POST['amount'];

// Fetch current balance


$result = $conn->query("SELECT balance FROM accounts WHERE id = $account_id");
$account = $result->fetch_assoc();

if ($transaction_type === 'withdrawal' && $amount > $account['balance']) {


echo "Insufficient funds.";
} else {
// Update balance
$new_balance = $transaction_type === 'deposit' ? $account['balance'] +
$amount : $account['balance'] - $amount;
$conn->query("UPDATE accounts SET balance = $new_balance WHERE id =
$account_id");

// Record transaction
$stmt = $conn->prepare("INSERT INTO transactions (account_id, transaction_type,
amount) VALUES (?, ?, ?)");
$stmt->bind_param("isd", $account_id, $transaction_type, $amount);
$stmt->execute();

echo "Transaction successful.";


}

transaction history-
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}

$conn = new mysqli("localhost", "root", "", "investment_platform");


$user_id = $_SESSION['user_id'];

// Fetch transactions
$transactions = $conn->query("SELECT t.transaction_date, a.account_type,
t.transaction_type, t.amount
FROM transactions t
JOIN accounts a ON t.account_id = a.id
WHERE a.user_id = $user_id
ORDER BY t.transaction_date DESC");
?>

<h2>Transaction History</h2>
<table border="1">
<tr>
<th>Date</th>
<th>Account Type</th>
<th>Transaction Type</th>
<th>Amount</th>
</tr>
<?php while ($transaction = $transactions->fetch_assoc()): ?>
<tr>
<td><?= $transaction['transaction_date'] ?></td>
<td><?= ucfirst($transaction['account_type']) ?></td>
<td><?= ucfirst($transaction['transaction_type']) ?></td>
<td>$<?= $transaction['amount'] ?></td>
</tr>
<?php endwhile; ?>
</table>

You might also like