CS521 Assignment-2 Solution:
1. Install MySQL
- Download and install the latest version of MySQL from [MySQL’s official
website](https://dev.mysql.com/downloads/).
- Configure MySQL and ensure the MySQL server is running.
2. Create a Table
Run the following SQL commands in your MySQL database to create the
required table:
```sql
CREATE DATABASE LoanDatabase;
USE LoanDatabase;
CREATE TABLE Loan_Application (
Applicant_Name VARCHAR(50),
Monthly_Income INT,
Employment_Type VARCHAR(50),
CNIC BIGINT,
Cell BIGINT,
Amount INT
);
```
3. PHP Form for Data Input:
Save the following PHP code as `loan_application.php` in your server
directory (e.g., `htdocs` for XAMPP):
```php
<?php
$servername = “localhost”;
$username = “root”; // Default for XAMPP
$password = “”; // Leave blank for XAMPP
$dbname = “LoanDatabase”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
If ($conn->connect_error) {
Die(“Connection failed: “ . $conn->connect_error);
If ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
$action = $_POST[‘action’];
$applicant_name = $_POST[‘applicant_name’];
$monthly_income = $_POST[‘monthly_income’];
$employment_type = $_POST[‘employment_type’];
$cnic = $_POST[‘cnic’];
$cell = $_POST[‘cell’];
$amount = $_POST[‘amount’];
If ($action === ‘save’) {
$sql = “INSERT INTO Loan_Application (Applicant_Name,
Monthly_Income, Employment_Type, CNIC, Cell, Amount)
VALUES (‘$applicant_name’, ‘$monthly_income’,
‘$employment_type’, ‘$cnic’, ‘$cell’, ‘$amount’)”;
} elseif ($action === ‘update’) {
$sql = “UPDATE Loan_Application
SET Monthly_Income=’$monthly_income’,
Employment_Type=’$employment_type’, Cell=’$cell’, Amount=’$amount’
WHERE CNIC=’$cnic’”;
} elseif ($action === ‘delete’) {
$sql = “DELETE FROM Loan_Application WHERE CNIC=’$cnic’”;
} elseif ($action === ‘search’) {
$sql = “SELECT * FROM Loan_Application WHERE CNIC=’$cnic’”;
$result = $conn->query($sql);
If ($result->num_rows > 0) {
Echo “<h3>Search Results:</h3>”;
While ($row = $result->fetch_assoc()) {
Echo “Name: “ . $row[“Applicant_Name”] . “, Income: “ .
$row[“Monthly_Income”] . “, Type: “ . $row[“Employment_Type”] . ", CNIC: " .
$row["CNIC"] . ”, Cell: “ . $row[“Cell”] . “, Amount: “ . $row[“Amount”] .
“<br>”;
} else {
Echo “No records found!”;
}
}
If ($action !== ‘search’) {
If ($conn->query($sql) === TRUE) {
Echo “Operation successful!”;
} else {
Echo “Error: “ . $conn->error;
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Bank Loan Application</title>
</head>
<body>
<h1>Bank Loan Application</h1>
<form method=”post” action=””>
<label>Applicant Name:</label>
<input type=”text” name=”applicant_name” required><br><br>
<label>Monthly Income:</label>
<input type=”number” name=”monthly_income” required><br><br>
<label>Employment Type:</label>
<input type=”text” name=”employment_type” required><br><br>
<label>CNIC:</label>
<input type=”number” name=”cnic” required><br><br>
<label>Cell#:</label>
<input type=”number” name=”cell” required><br><br>
<label>Loan Amount:</label>
<input type=”number” name=”amount” required><br><br>
<button type=”submit” name=”action” value=”save”>Save</button>
<button type=”submit” name=”action”
value=”update”>Update</button>
<button type=”submit” name=”action”
value=”delete”>Delete</button>
<button type=”submit” name=”action”
value=”search”>Search</button>
</form>
</body>
</html>
```
4. Explanation of Buttons
- Save: Inserts a new record into the database.
- Update: Modifies an existing record based on CNIC.
- Delete: Removes a record matching the given CNIC.
- Search: Displays details of the applicant matching the CNIC.
5. Run the Application
- Place the `loan_application.php` file in the appropriate directory (e.g.,
`htdocs` for XAMPP or `/var/www/html` for Apache).
- Access the form via your browser: `http://localhost/loan_application.php`.