Jump to: navigation, search

Php oop product management product update

From w3cyberlearnings

Contents

Objective

This page uses to edit or update the product based on the product's id.

edit.php

Before we can update or edit product we need to get the product first. We get the product based on the product id.

<?php
require_once 'product.php';

if (isset($_POST['update'])) {
	$prod = new product();
	$prod->id = $_POST['id'];
	$prod->pname = $_POST['name'];
	$prod->price = $_POST['price'];
	$prod->seller = $_POST['seller'];
	if ($prod->update_db()) {
		header('Location: detail.php?id=' . $_POST['id']);
	} else {
		header('Location:list.php');
	}
}
?>


<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Edit</title>
	</head>
<body>
<?php
if (!empty($_REQUEST['id'])) {
	$product1 = new product();
	$product1->id = $_REQUEST['id'];

	$produc = $product1->get_product_by_id();
	$product = $produc[0];
} else {
	header('Location:list.php');
}
?>

<form method="post" action="edit.php">

<table border="0">
	<tr>
		<td colspan="2">Update Product Information</td>
	</tr>
	<tr>
		<td>Product name:</td>
		<td>
			<input type="text" name="name" size="20" 
			maxlength="20" value="<?php echo $product['pname']; ?>"/>
		</td>
	</tr>
	<tr>
		<td>Price ($$$):</td>
		<td><input type="text" name="price" size="3" 
			maxlength="5" value="<?php echo $product['price']; ?>"/>
		</td>
	</tr>
	<tr>
		<td>Seller:</td>
		<td><input type="text" name="seller" 
		size="20" maxlength="20" value="<?php echo $product['seller']; ?>"/>
		</td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="update" value="Update"  /></td>
	</tr>
</table>
<input type="hidden" name="id" value="<?php echo $product['id']; ?>"/>
</form>
<?php require_once 'menu.php'; ?>
</body>
</html>

get product based on the product id

When the product id is not there we redirect user to the list.php page.

<?php
if (!empty($_REQUEST['id'])) {
	$product1 = new product();
	$product1->id = $_REQUEST['id'];

	$produc = $product1->get_product_by_id();
	$product = $produc[0];
} else {
	header('Location:list.php');
}


// access product by associative array
/*
$product['pname']   // product name
$product['price']   // product price
*/


?>

display HTML edit product form

We generate a HTML form for edit product and we hide the product id in the hidden field.

<form method="post" action="edit.php">

<table border="0">
   <tr>
	<td colspan="2">Update Product Information</td>
   </tr>
   <tr>
	<td>Product name:</td>
	<td>
		<input type="text" name="name" size="20" 
maxlength="20" value="<?php echo $product['pname']; ?>"/>
	</td>
   </tr>
   <tr>
	 <td>Price ($$$):</td>
	 <td><input type="text" name="price" size="3" 
maxlength="5" value="<?php echo $product['price']; ?>"/></td>
   </tr>
   <tr>
	<td>Seller:</td>
        <td><input type="text" name="seller" size="20" 
maxlength="20" value="<?php echo $product['seller']; ?>"/></td>
   </tr>
   <tr>
	<td></td>
	<td><input type="submit" name="update" value="Update"  /></td>
   </tr>
</table>
   <input type="hidden" name="id" value="<?php echo $product['id']; ?>"/>
</form>
<?php require_once 'menu.php'; ?> 

process HTML form

After user make all the changes, now it is time to process the user input data and update the record accordingly.

<?php
require_once 'product.php';

if (isset($_POST['update'])) {
	$prod = new product();
// assign a new record
	$prod->id = $_POST['id'];
	$prod->pname = $_POST['name'];
	$prod->price = $_POST['price'];
	$prod->seller = $_POST['seller'];
// call the update product 
	if ($prod->update_db()) {
                // after update the product, we redirect user to the product detail
		header('Location: detail.php?id=' . $_POST['id']);
	} else {
                // if we can not update the record, we display the product list to the user
		header('Location:list.php');
	}
}
?>

Print Screen

Php oop product management product update.png

Related Links


  1. Chapter 1: Singleton class
  2. Chapter 2: Table
  3. Chapter 3: Product Class
  4. Chapter 4: Add Product
  5. Chapter 5: Activity Menu
  6. Chapter 6: List All Products
  7. Chapter 7: Product Detail
  8. Chapter 8: Update or Edit Product
  9. Chapter 9: Delete Product
Navigation
Web
SQL
MISC
References