PHP PROGRAM
Perform delete and update operation on account table |PHP Program|
Introduction
When it comes to managing account records in a database, being able to update and delete entries is essential. In this blog post, we’ll look at how to perform these operations using PHP. We’ll cover a simple web interface that allows users to update the balance of an account or delete an account entirely. The operations are performed on a PostgreSQL database.
Overview of the Program
The PHP program consists of three main components:
Home.html: The homepage provides options for the user to either delete or update an account record.
update.php: This script handles updating the balance of a specific account.
delete.php: This script is responsible for deleting an account from the database.
Home.html: The Main Interface
The homepage presents two buttons, one for deleting and the other for updating an account. Clicking either button will redirect the user to the corresponding form.
<html>
<head>
</head>
<body>
<center>
Click the operation you want to perform...<br>
<form action="delete.php">
<input type="submit" value="Delete"><br>
</form>
<form action="update.php">
<input type="submit" value="Update"><br>
</form>
</center>
</body>
</html>
This simple interface allows users to choose the operation they want to perform.
update.php: Updating an Account Balance
This script first presents a form where the user can enter the account number and the new balance. Upon submission, the script connects to the PostgreSQL database, updates the balance, and displays the updated records.
<html>
<body>
<form action="" method="post">
Account No: <input type="number" name="accno"><br>
Amount: <input type="number" name="amount"><br>
<input type="submit" value="Update"><br>
</form>
</body>
</html>
<?php
$accno = $_POST["accno"];
$amount = $_POST["amount"];
$con = pg_connect("host=localhost dbname=college user=postgres password=TIGER");
if (!$con) {
echo "Couldn't connect to the database!";
} else {
$qry1 = "select * from account";
$qry2 = "update account set amount=$amount where accno=$accno";
$result1 = pg_query($con, $qry1);
$result2 = pg_query($con, $qry2);
$result1 = pg_query($con, $qry1);
while ($row = pg_fetch_row($result1)) {
echo "<br>\n";
echo "accno: $row[0] name: $row[1] balance: $row[2]";
}
}
?>
delete.php: Deleting an Account
Similar to the update script, the delete script presents a form where the user can enter the account number of the record they wish to delete. Once submitted, the script removes the record from the database and displays the remaining records.
delete.php
<html> <body> <form action="" method="post"> Account No: <input type="number" name="accno"><br> <input type="submit" value="Delete"> </form> </body> </html> <?php $accno = $_POST["accno"]; $con = pg_connect("host=localhost dbname=college user=postgres password=TIGER"); if (!$con) { echo "Couldn't connect to the database!"; } else { $qry1 = "select * from account"; $qry2 = "delete from account where accno=$accno"; $result1 = pg_query($con, $qry1); $result2 = pg_query($con, $qry2); $result1 = pg_query($con, $qry1); while ($row = pg_fetch_row($result1)) { echo "<br>\n"; echo "accno: $row[0] name: $row[1] balance: $row[2]"; } } ?>
Output
I have a table 'account' with some data
See this below UIs for the ouptut
Step-by-Step Guide
Update Operation:
The user enters an account number and the new balance amount.
The PHP script updates the balance in the database and displays all the account records to confirm the update.
Delete Operation:
The user inputs the account number they wish to delete.
The PHP script deletes the record associated with that account number and then shows the remaining records.
Conclusion
This blog post demonstrates how to perform basic delete and update operations on an account table using PHP. By understanding these fundamentals, you can further expand the program to include additional features like input validation, error handling, and even more complex database operations. This simple yet effective program is a great starting point for managing account records in a web-based application.
Happy coding!
Post a Comment
0 Comments