Simple php inventory system

This is the third part of the six part series about creating a php online store.

Here’s a summary of the

Just in case, I haven’t included the download link on the database that were going to use. Here it is. Download that and import it into any database that you have created. Using phpmyadmin.

 

 

Item Creation

This would be the form that will be used by the admin to create new products.

Go ahead and type the codes below. Or just copy and paste it into your text editor:

<form action="regprodact.php"  id="info" method="post">
   <h3>Product Registration</h3>
 
 
      <label for="prodname">Product Name:</label>
      <input type="text" id="prodname" name="prodname" class="validate[required]">
 
 
      <label for="description">Description:</label>
      <input type="text" id="description" name="description" class="validate[required]">
 
 
      <label for="reorder">Reorder Qty:</label>
      <input type="text" id="reorder" name="reorder" class="validate[required,custom[onlyNumber]]">
      
 
    
      <label for="bprice">Cost price:</label>
      <input type="text" id="pprice" name="pprice" value="" class="validate[required,custom[onlyNumber]]" value="">
 
      <label for="sprice">Unit price:</label>
      <input type="text" id="sprice" name="sprice" value="" class="validate[required,custom[onlyNumber]]" value="">
  
   
      <label for="category">Category:</label>
      <input type="text" id="category" name="category"/>
 
<input type="submit" value="create"/>
</form>

It’s a good practice not to make the value of the submit button into something like ‘submit’. Because that doesn’t give the user any idea on what he is doing. Most of the time you have to make use of an action word or a verb in the value of the submit button.

Next thing that you have to do is to create a new php file and name it to regprodact.php or whatever form action you have included in the item creation form. What this will do is to insert the data that is inputted by the user into the database.

<?php include('conn.php'); ?>
 
<?php 
 
$prod=$_POST['prodname'];
$desc=$_POST['description'];
$category=$_POST['category'];
 
$reorder=$_POST['reorder'];
 
$cost_price=$_POST['pprice'];
$unit_price=$_POST['sprice'];
 

/* format the text*/
$f_prod=ucwords(strtolower($prod));
$f_desc=ucwords(strtolower($desc));
$f_category=ucwords(strtolower($category));
 
$inserts=mysql_query("INSERT INTO prod_table(PRODUCT, P_DESC, CATEGORY, REORDER_LVL, B_PRICE, S_PRICE") VALUES('$f_prod','$f_desc','$f_category', '$reorder', '$cost_price', '$unit_price')");
 
if(!$inserts){
    echo mysql_error();
}
?>

 

Item list

This will be used by the user to (surprise!) list all the items that matches the query that he is going to type in the textbox:

<form action="listprod.php" method="post">
<label for="items">Item name:</label>
<input type="text" name="items"/>
<input type="submit" value="list">
</form>

Create a new php file again, and name it to listprod.php. This is the php file that would produce the results from the user query.

<?php include('conn.php'); ?>
<?php
$item=$_POST['items'];
$list=mysql_query("SELECT * FROM prod_table WHERE PRODUCT LIKE '$item%'");
 
if(mysql_num_rows($list)==0){
 
}else{
 
?>
<table border="1">
<tr>
<th>ITEM ID</th>
<th>DESCRIPTION</th>
<th>CATEGORY</th>
<th>QTY AVAILABLE</th>
<th>REORDER LEVEL</th>
<th>COST PRICE</th>
<th>UNIT PRICE</th>
</tr>
 
<?php
while($row=mysql_fetch_assoc($list)){
?>
 
<tr>
<td><?php echo $row['PID']; ?></td>
<td><?php echo $row['PRODUCT']; ?></td>
<td><?php echo $row['P_DESC']; ?></td>
<td><?php echo $row['CATEGORY']; ?></td>
<td><?php echo $row['QTYHAND']; ?></td>
<td><?php echo $row['REORDER_LVL']; ?></td>
<td><?php echo $row['B_PRICE']; ?></td>
<td><?php echo $row['S_PRICE']; ?></td>
</tr>
</table>
<?php
}
 
}
?>

 

Updating Items

This will be used by the user to update the items that are already stored in the database. You can integrate it with the list item module so that the user can search the items that he is going to update. But of course, I’ll leave that to you so that you’ll also have something to do.

Create a new php file again and name it to updateform.php. This would be use by the user to edit the existing item data that is stored on the database. I’ll leave it as a challenge on how you can connect this module with the list module.

The following are the codes for updateform.php:

<?php include('conn.php'); ?>
<?php 
 
$item_id=$_GET['item_id']; 
 
$listupdate=mysql_query("SELECT * FROM prod_table WHERE PID='$item_id'"); 
 
 
 
/*You'll have to do the rest here*/
 
?>

Again, I’ll be leaving you with some challenge. The codes that are supposed to be placed after the comment is similar to the codes used for listing item data. The only difference is that it will only return 1 record. And were going to make use of textbox instead of a table. Remember that a text box should be inside a form tag, and the form tag should have 2 attributes: action and method.

 

Now, you have to create another php file and name it to whatever form action you have indicated in updateform.php. Here are the codes:

<?php
/*Something is missing in this part, I wonder what it is? */
 
$pid=$_POST['prodid'];
$prod=$_POST['prodname'];
$desc=$_POST['description'];
$category=$_POST['category'];
 
 
$qtyonhand=$_POST['qtyonhand'];
$pprice=$_POST['pprice'];
$sprice=$_POST['sprice'];
$reorder=$_POST['reorder'];
 
/* format the text*/
$f_prod=ucwords(strtolower($prod));
$f_desc=ucwords(strtolower($desc));
$f_category=ucwords(strtolower($category));
 
$update_item=mysql_query("UPDATE prod_table SET PRODUCT='$f_prod', P_DESC='$f_desc', CATEGORY='$f_category' , QTYHAND='$qtyonhand', B_PRICE='$pprice', S_PRICE='$sprice', REORDER_LVL='$reorder' WHERE PID='$pid'");
 
if(!$update_item){
    echo mysql_error();
}
 
?>

 

Deleting Items

The last module that were going to create in this tutorial.  I’m getting lazy now, so here’s the query. Remember to do some housekeeping. This is not the full code:

DELETE FROM prod_table WHERE PID='$pid'

Changed my mind. This module should also be connected to the list item module. The most common that people do is to add a delete icon on the table that has the items listed on it. And put a link on that icon to whatever the name of the php file which performs the delete query. Add something like this to your list module:

<a href="delprod.php?itemid=<?php echo $row['PID']; ?>">

<img src="deleteicon.png"></img></a>

Common sense tells us that this link would yield a GET method. So here’s the more complete code on delprod.php or whatever you call it on the link that you have created:

<?php
$item_id=$_GET['itemid'];
 
$deletes=mysql_query("DELETE FROM prod_table WHERE PID='$item_id'");
 
/*do some housekeeping here*/
?>

You might have notice that the parameter for the GET method should be the same as the variable that you have declared on your url. In this case it is ‘itemid’. Always remember that since the code won’t work if they are not the same.

 

Conclusion

That’s it for the inventory tutorial. Next time I’m going to tell you about the cart. The meat of this six part tutorial series.

17 thoughts on “Simple php inventory system

  1. am i going to save the forms as php file???

    also idk about the textbox, teach me pls..

    also i dunno what “Now, you have to create another php file and name it to whatever form action you have indicated in updateform.php.” means..

    • it means that if you have a form:

      you have to create a file and name it actionfile.php that will serve as the form which will save the contents of your input into the database.

  2. can i have a copy of the challenge u gave in update form? sorry tho but i am not that really good yet in php, i cannot figure out the codes on my own. tnx!

  3. when i click the listprod.php link it says:

    Warning: include(conn.php) [function.include]: failed to open stream: No such file or directory in D:\wamp\www\New folder (2)\listprod.php on line 1

    Warning: include() [function.include]: Failed opening ‘conn.php’ for inclusion (include_path=’.;C:\php\pear’) in D:\wamp\www\New folder (2)\listprod.php on line 1

    Notice: Undefined index: items in D:\wamp\www\New folder (2)\listprod.php on line 3

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\wamp\www\New folder (2)\listprod.php on line 6

    what is the problem here?

  4. do you have an php that decrements the quantity by 1 if the product is been buy,,,,,,,,,,or the incrementation of the quantity…tnx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s