View and Edit Items


View and edit of items are handled in "view_items.php" file. We link this file in the "View Items" sub menu in "Inventory" main menu. When visit to the page, it shows all items we created. When click on "Edit" link, item editing panel shows in the right side panel. That because when you click on "edit" link, it refer to the same page, but with a url parameter "id" and with a value of the product id of the database table.

In PHP we write argument to show the editing form, only if this URL parameter is passed to the page.

Source code of "view_items.php" page:

<?php include("inc/page_header.php"); ?>
<?php
 if(check('Manage Inventory') != 'Valid'){
  header("Location: sys_home.php?pre=error");
  ob_end_flush();
 }
?>

<script type="text/javascript">
function newCat(){
 var catname=prompt("New Category Name: ");
 if(catname!=null && catname!=""){
  window.location="func/new_category.php?str="+catname;
 }
}
</script>
 <div class="wrapper">

  <div class="s60">
  <div class="boxtitle">View Inventory Items</div>
   <table width="97%" cellpadding="3px" cellspacing="1px">
    <tr id="headrow"><td>Item Code</td><td>Item Name</td><td>Category</td><td>Act</td></tr>
    <?php
     $q = mysql_query("SELECT * FROM items, categories WHERE Item_Category = Category_ID ORDER BY Item_Name");
     while($r = mysql_fetch_assoc($q)){
      echo "<tr id=\"sh3\"><td>{$r['Item_Part_No']}</td><td>{$r['Item_Name']}</td><td>{$r['Category_Name']}</td><td><a href=\"view_items.php?id={$r['Item_Code']}\">Edit</a></td></tr>";
     } 
    ?>
   </table>
  </div>
  <div class="s40">
   <p><strong>Edit Items<br>
   </strong>Use this form to edit item details. Please select an item from the left side list to edit.</p>
   <?php
    if(isset($_GET['id'])){
     $itemcode = $_GET['id'];
     $q2 = mysql_query("SELECT * FROM items WHERE Item_Code = '$itemcode'");
     $r2 = mysql_fetch_assoc($q2);
    }
    // form handling on submit
     if(isset($_POST['submit'])){
      $item_name = $_POST['item_name'];
      $part_no = $_POST['part_no'];
      $cat = $_POST['cat'];
      $low = $_POST['lowlimit'];
      $unit = $_POST['unit'];
      
      if($item_name == ''){ $error = "Please enter Item Name!"; }
      if($cat == '0'){ $error = "Please select a category!"; }
      if($low == '' || $low == '0'){ $error = "Please enter a valid low limit!"; }
      if($unit == '' || $unit == '0'){ $error = "Please enter a valid unit!"; }
      // duplicates
      $q = mysql_query("SELECT * FROM items WHERE (Item_Name = '$item_name' OR Item_Part_No = '$part_no') AND Item_Code != '$itemcode'");
      if(mysql_num_rows($q) > 0){
       $error = "This item is already in the inventory!";
      }
      
      if(!isset($error)){
       mysql_query("
        UPDATE items SET 
         Item_Name = '$item_name', 
         Item_Part_No = '$part_no', 
         Item_Category = '$cat', 
         Low_Limit = '$low', 
         Unit = '$unit' 
        WHERE Item_Code = '$itemcode'
       ");
       $noerror = "Item details has been updated! <a href='view_items.php'>Reload Data</a>";
      }
     }
    // end of form handling
     if(isset($error)){
      echo "<div class=\"errordiv\">{$error}</div>";
     }
     if(isset($noerror)){
      echo "<div class=\"noerrordiv\">{$noerror}</div>";
     }
   ?>
   <?php if(isset($_GET['id']) && !isset($noerror)){ ?>
   <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
   <table>
    <tr>
     <td>Item Name:</td>
     <td><input type="text" size="37" name="item_name" value="<?php if(isset($_GET['id'])){ echo $r2['Item_Name']; } ?>"></td>
    </tr>
    <tr>
     <td>Part No:</td>
     <td><input type="text" size="20" name="part_no" value="<?php if(isset($_GET['id'])){ echo $r2['Item_Part_No']; } ?>"></td>
    </tr>
    <tr>
     <td>Category:</td>
     <td>
      <select name="cat" size="1">
       <option value="0">Please select...</option>
       <?php
        $q = mysql_query("SELECT * FROM categories ORDER BY Category_Name");
        while($r = mysql_fetch_assoc($q)){
         echo "<option value=\"{$r['Category_ID']}\" ";
         if($r['Category_ID'] == $r2['Item_Category']){ echo "selected"; }
         echo ">{$r['Category_Name']}</option>\n";
        }
       ?>
      </select><br> <a href="javascript: newCat()">Add New Category</a>
     </td>
    </tr>
    <tr>
     <td>Unit</td>
     <td><input type="text" size="10" name="unit" value="<?php if(isset($_GET['id'])){ echo $r2['Unit']; } ?>"></td>
    </tr>
    <tr>
     <td>Low Limit:</td>
     <td><input type="text" size="10" name="lowlimit" value="<?php if(isset($_GET['id'])){ echo $r2['Low_Limit']; } ?>"></td>
    </tr>
    <tr><td>&nbsp;</td><td><input class="btn" type="submit" name="submit" value="Update Item"></td></tr>
   </table>
   </form>
   <?php
    // end of if(isset($_GET['id'])){ of editing form
    }
   ?>
  </div>
  
 </div>
 
<?php include("inc/page_footer.php"); ?>

Comments