Product Category Management


Creating a new product category, view and edit product categories all are handled in a single file "categories.php" and you can access to this page under "Inventory" main menu, "Categories" sub menu. Here also you can only create and edit categories and delete function not provided. You can add this function and improve the system.

Source code of "categories.php" file

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

 <div class="wrapper">

  <div class="s60">
  <div class="boxtitle">Categories</div>
   <div style="border:1px solid #FFFF99; padding:3px; margin:5px; ">
   <p><strong>New Category<br>
   </strong>Use this form to create a new category.</p>

   <?php
    // form handling on submit
     if(isset($_POST['submit'])){
      $catname = $_POST['name'];
      
      if($catname == ''){ $error = "Please enter Category Name!"; }
      // duplicates
      $q = mysql_query("SELECT * FROM categories WHERE Category_Name = '$catname'");
      if(mysql_num_rows($q) > 0){
       $error = "This category name is already exists in the system!";
      }
      
      if(!isset($error)){
       mysql_query("INSERT INTO categories (Category_Name) VALUES ('$catname')");
       $noerror = "Category has been added to the system!";
      }
     }
    // 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>Category Name:</td>
     <td><input type="text" size="40" name="name" value="<?php if(isset($_POST['submit']) && isset($error)){ echo $catname; } ?>"></td>
    </tr>
    <tr><td>&nbsp;</td><td><input class="btn" type="submit" name="submit" value="Add Category"></td></tr>
   </table>
   </form>
   </div>
   <p>&nbsp;</p>
   <p><strong>Edit Category<br>
   </strong>Use this form to edit categories. Please select a category from the right side list to edit.</p>
   <?php
    if(isset($_GET['id'])){
     $catid = $_GET['id'];
     $q = mysql_query("SELECT * FROM categories WHERE Category_ID = '$catid'");
     $r = mysql_fetch_assoc($q);
     $catname = $r['Category_Name'];
    }
    if(isset($_POST['submit2'])){
     $catname = $_POST['catname'];
     if($catname == ''){ $error2 = "Please enter Category Name!"; }
     // duplicates
     $q = mysql_query("SELECT * FROM categories WHERE Category_Name = '$catname' AND Category_ID != '$catid'");
     if(mysql_num_rows($q) > 0){
      $error2 = "This category name is already exists in the system!";
     }
     if(!isset($error2)){
      mysql_query("UPDATE categories SET Category_Name = '$catname' WHERE Category_ID = '$catid'");
      $noerror2 = "Category has been updated!";
     }
    } 
    if(isset($error2)){
     echo "<div class=\"errordiv\">{$error2}</div>";
    }
    if(isset($noerror2)){
     echo "<div class=\"noerrordiv\">{$noerror2}</div>";
    }
   ?>
   <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
   <table>
    <tr>
     <td>Category Name:</td>
     <td><input type="text" size="40" name="catname" value="<?php if(isset($_GET['id'])){ echo $catname; } ?>"></td>
    </tr>
    <tr><td>&nbsp;</td><td><input class="btn" type="submit" name="submit2" value="Update Category"></td></tr>
   </table>
   <input type="hidden" name="catid" value="<?php if(isset($_GET['id'])){ echo $catid; } ?>">
   </form>

  </div>
  <div class="s40">
   <div style="overflow:scroll; width:333px; height:455px;">
   <table width="97%" cellpadding="3px" cellspacing="1px">
    <tr id="headrow"><td>Category ID</td><td>Cateogry Name</td><td>Actions</td></tr>
    <?php
     $q = mysql_query("SELECT * FROM categories ORDER BY Category_ID");
     while($r = mysql_fetch_assoc($q)){
      echo "<tr id=\"sh0\"><td>{$r['Category_ID']}</td><td>{$r['Category_Name']}</td><td><a href=\"categories.php?id={$r['Category_ID']}\">Edit</a></td></tr>";
     } 
    ?>
   </table>
   </div>
  </div>
  
 </div>
 
<?php include("inc/page_footer.php"); ?>

Comments