Php oop product management product add
From w3cyberlearnings
Contents |
Objective
Create a new product page.
add.php
We create this add.php page to process the user input for a particular product. This page generates the HTML form as well as process the form input.
<?php require_once 'product.php'; $message = ""; if (!empty($_POST['name']) && !empty($_POST['price']) && !empty($_POST['seller'])) { $product1 = new product(); $product1->pname = $_POST['name']; $product1->price = $_POST['price']; $product1->seller = $_POST['seller']; $id = $product1->insert_db(); if ($id) { $message = '<li> Id:' . $id . '</li>'; $message .= '<li> Product Name:' . $_POST['name'] . '</li>'; $message .= '<li> Price: ' . $_POST['price'] . '</li>'; $message .= '<li> Seller: ' . $_POST['seller'] . '</li>'; } } if ($message == "") { $message = 'No data! Please enter a new record!'; } else { $message = '<ul>' . $message . '</ul>'; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Add Product</title> </head> <body> <h3><?php echo $message; ?></h3> <form method="post" action="add.php"> <table> <tr> <td colspan="2">Add Product Information</td> </tr> <tr> <td>Product name:</td> <td> <input type="text" name="name" size="20" maxlength="20"/> </td> </tr> <tr> <td>Price ($$$):</td> <td><input type="text" name="price" size="3" maxlength="5"/></td> </tr> <tr> <td>Seller:</td> <td><input type="text" name="seller" size="20" maxlength="20"/></td> </tr> <tr> <td></td> <td> <input type="submit" name="add" value="Add" /> </td> </tr> </table> </form> <a href="list.php">Product List</a> </body> </html>
Process HTML form
This is the portion of the PHP program that process the HTML form.
<?php require_once 'product.php'; $message = ""; // check for user input and make sure it is not empty if (!empty($_POST['name']) && !empty($_POST['price']) && !empty($_POST['seller'])) { // when user submit the form // create a product object $product1 = new product(); $product1->pname = $_POST['name']; // assign product name $product1->price = $_POST['price']; // assign price $product1->seller = $_POST['seller'];// assign seller $id = $product1->insert_db(); // call insert to insert record to the table // display record after inserted if ($id) { $message = '<li> Id:' . $id . '</li>'; $message .= '<li> Product Name:' . $_POST['name'] . '</li>'; $message .= '<li> Price: ' . $_POST['price'] . '</li>'; $message .= '<li> Seller: ' . $_POST['seller'] . '</li>'; } } if ($message == "") { $message = 'No data! Please enter a new record!'; } else { $message = '<ul>' . $message . '</ul>'; } ?>