@ 2024-11-30T02:35:59Z <?php
// Step 1: Connect to the database
$conn = new mysqli("localhost", "root", "", "customer_db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Step 2: Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$customer_name = $_POST['customer_name'];
$email = $_POST['email'];
$phone_number = $_POST['phone_number'];
$account_type = $_POST['account_type'];
$principal = $_POST['principal'];
// Calculate interest based on account type
$interest = 0;
if ($account_type == "Saving") {
$interest = $principal * 0.06; // 6% interest
} elseif ($account_type == "Fixed Deposit") {
$interest = $principal * 0.10; // 10% interest
}
// Insert data into the database
$sql = "INSERT INTO customers (customer_name, email, phone_number, account_type, principal, interest)
VALUES ('$customer_name', '$email', '$phone_number', '$account_type', '$principal', '$interest')";
if ($conn->query($sql) === TRUE) {
echo "Customer data inserted successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Customer Management</title>
</head>
<body>
<h1>Customer Management Form</h1>
<!-- Step 3: Form to input data -->
<form method="POST" action="">
<label>Customer Name:</label>
<input type="text" name="customer_name" required><br><br>
<label>Email:</label>
<input type="email" name="email" required><br><br>
<label>Phone Number:</label>
<input type="text" name="phone_number" required><br><br>
<label>Account Type:</label>
<select name="account_type" required>
<option value="Saving">Saving</option>
<option value="Current">Current</option>
<option value="Fixed Deposit">Fixed Deposit</option>
</select><br><br>
<label>Principal Amount:</label>
<input type="number" name="principal" required><br><br>
<button type="submit">Submit</button>
</form>
<h2>Customer Details</h2>
<!-- Step 4: Display data -->
<?php
$sql = "SELECT * FROM customers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1'>
<tr>
<th>Customer Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Account Type</th>
<th>Principal</th>
<th>Interest</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['customer_name']}</td>
<td>{$row['email']}</td>
<td>{$row['phone_number']}</td>
<td>{$row['account_type']}</td>
<td>{$row['principal']}</td>
<td>{$row['interest']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No customer records found!";
}
$conn->close();
?>
</body>
</html>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.2.0 - 8.2.27 , 8.3.0 - 8.3.15 , 8.4.1 - 8.4.2 Fatal error: Uncaught Error: Class "mysqli" not found in /in/0cJag:3
Stack trace:
#0 {main}
thrown in /in/0cJag on line 3
Process exited with code 255 . preferences:dark mode live preview
55.09 ms | 406 KiB | 5 Q