Create New Product


You can go to "New Item" sub menu under "Inventory" main menu to create new products / items. This menu is linked to "new_item.php" file. This file also includes a side panel to show all created items. Item Code is unique and can not have to same value twice. Since it is not enforced in the MySQL database, here we checked it in PHP code.

Following the source code of "new_item.php" file:

<?php include("inc/page_header.php"); ?>
<?php
 // check previleges
 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">New Item</div>
   <p><strong>Add New Item<br>
   </strong>Use this form to add new items to the inventory.</p>

   <?php
    // form handling on submit
     if(isset($_POST['submit'])){
      $item_name = $_POST['item_name'];
      $cat = $_POST['cat'];
      $low = $_POST['lowlimit'];
      $part_no = $_POST['part_no'];
      $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'");
      if(mysql_num_rows($q) > 0){
       $error = "This item is already in the inventory!";
      }
      
      if(!isset($error)){
       mysql_query("
        INSERT INTO items
         (Item_Name, Item_Part_No, Item_Category, Low_Limit, Unit) 
        VALUES ('$item_name', '$part_no', '$cat', $low, '$unit') 
       ");
       $noerror = "Item details has been added to the inventory!";
      }
     }
    // end of form handling
     if(isset($error)){
      echo "<div class=\"errordiv\">{$error}</div>";
     }
     if(isset($noerror)){
      echo "<div class=\"noerrordiv\">{$noerror}</div>";
     }
   ?>
   <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
   <table>
    <tr>
     <td>Item Name:</td>
     <td><input type="text" size="40" name="item_name" value="<?php if(isset($_POST['submit']) && isset($error)){ echo $item_name; } ?>"></td>
    </tr>
    <tr>
     <td>Item Code:</td>
     <td><input type="text" size="20" name="part_no" value="<?php if(isset($_POST['submit']) && isset($error)){ echo $part_no; } ?>"></td>
    </tr>
    <tr>
     <td>Category:</td>
     <td>
      <select name="cat" size="1">
       <option value="0" selected>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']}\">{$r['Category_Name']}</option>\n";
        }
       ?>
      </select> &nbsp; &nbsp; <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($_POST['submit']) && isset($error)){ echo $unit; }else{ echo ""; } ?>"></td>
    </tr>
    <tr>
     <td>Low Limit:</td>
     <td><input type="text" size="10" name="lowlimit" value="<?php if(isset($_POST['submit']) && isset($error)){ echo $low; }else{ echo "1"; } ?>"></td>
    </tr>
    <tr><td>&nbsp;</td><td><input class="btn" type="submit" name="submit" value="Add Item"></td></tr>
   </table>
   </form>
  </div>
  <div class="s40">
   <div style="overflow:scroll; width:333px; height:455px;">
   <table width="97%" cellpadding="3px" cellspacing="1px">
    <tr id="headrow"><td>Item Code</td><td>Item Name</td><td>Category</td></tr>
    <?php
     $q = mysql_query("SELECT * FROM items, categories WHERE Item_Category = Category_ID ORDER BY Item_Code");
     while($r = mysql_fetch_assoc($q)){
      echo "<tr id=\"sh0\"><td>{$r['Item_Part_No']}</td><td>{$r['Item_Name']}</td><td>{$r['Category_Name']}</td></tr>";
     } 
    ?>
   </table>
   </div>
  </div>
  
 </div>
 
<?php include("inc/page_footer.php"); ?>

Comments