PHP PROGRAM
Program to perform insert and display operations |PHP Program|
Introduction
In any retail or manufacturing environment, managing inventory is a critical task. Keeping track of stock levels, product information, and pricing ensures that the business operates smoothly and efficiently. In this blog post, we'll explore a PHP program that allows you to perform basic inventory management tasks such as inserting new stock information and displaying the current inventory.
The PHP Program: Inventory Management
This PHP program comprises two main components:
inventory.html: A simple HTML form that collects product information.
inventory.php: A PHP script that handles the insertion of data into a PostgreSQL database and displays the inventory data.
Let’s break down each component.
inventory.html: The Input Form
The HTML form provides a user interface for entering product information such as Product ID, Name, Quantity, and Rate. It also includes two buttons—one for inserting data into the database and another for displaying the current inventory.
<html>
<head>
<title>Inventory Management</title>
<center><h1>Stock Details</h1></center>
</head>
<body>
<center>
<form action="inventory.php" method="post">
Product ID: <input type="text" name="pid"><br>
Product Name: <input type="text" name="name"><br>
Quantity: <input type="text" name="quantity"><br>
Product Rate: <input type="text" name="rate"><br>
<input type="submit" name="btn" value="Insert">
<input type="submit" name="btn" value="Show">
</form>
</center>
</body>
</html>
This form captures the necessary details for managing inventory. Users can enter the product information and choose whether to insert the data or view the current stock.
inventory.php: Handling Insert and Display Operations
The PHP script processes the form data, connects to a PostgreSQL database, and performs the necessary operations based on the user's choice.
<?php
$prodid = $_POST["pid"];
$pname = $_POST["name"];
$quant = $_POST["quantity"];
$prate = $_POST["rate"];
switch ($_POST["btn"]) {
case "Insert":
$con = pg_connect("host=localhost dbname=product user=postgres password=tiger");
if (!$con) {
echo "Couldn't connect";
} else {
$sql = "insert into stock(id, name, quantity, rate) values('$prodid','$pname','$quant','$prate')";
$query = pg_query($con, $sql);
if (!$query) {
echo "Could not insert data";
} else {
echo "Successfully inserted data";
}
pg_close($con);
}
break;
case "Show":
$con = pg_connect("host=localhost dbname=product user=postgres password=tiger");
if (!$con) {
echo "Couldn't connect";
} else {
$sql = "select * from stock";
$result = pg_query($con, $sql);
echo "<table border=1>
<tr><th>ID</th>
<th>Name</th>
<th>Quantity</th>
<th>Rate</th></tr>";
while ($ds = pg_fetch_array($result)) {
echo "<tr>";
echo "<td>".$ds[0]."</td>";
echo "<td>".$ds[1]."</td>";
echo "<td>".$ds[2]."</td>";
echo "<td>".$ds[3]."</td>";
echo "</tr>";
}
echo "</table>";
pg_close($con);
}
break;
}
?>
Output
Explanation
1. Insert Operation:
When the user clicks the "Insert" button, the PHP script connects to the PostgreSQL database.
The script then inserts the product information into the stock table using an SQL INSERT statement.
If the insertion is successful, a success message is displayed. Otherwise, an error message is shown.
2. Show Operation:
When the "Show" button is clicked, the script fetches all records from the stock table using an SQL SELECT statement.
The results are displayed in an HTML table, showing all the products currently in the inventory.
Setting Up Your Database
Before running this PHP program, ensure that you have set up a PostgreSQL database with a stock table.
Ensure you replace the connection string in the PHP script with your actual database credentials.
Conclusion
Managing inventory is a crucial task in any business, and PHP provides a simple yet effective way to handle basic inventory operations. This program demonstrates how to insert new product information into a database and display the current inventory. By understanding these fundamentals, you can expand this program into a fully-featured inventory management system with additional features such as updating and deleting records, handling multiple users, and generating reports.
Happy coding!
Post a Comment
0 Comments