NameSilo

Php code for inventory system in updating item quantity

Spaceship Spaceship
Watch

cotton

Established Member
Impact
0
[RESOLVED]php code for inventory system in updating item quantity

recently,im developing an inventory system. the problem is i still dont know how to automatically update the quantity in the stock after the user take the items and how to add the item to the cart as in a shopping cart system. pls help..im using easyphp and i do not use class and function in my system.help me pls...

this is the sample of my code:
<table width="80%" border="1" align="center" cellpadding="0" cellspacing="1" bordercolor="#CCCCCC">
<tr>
<td width="18%" bgcolor="#E0E2EB"><div align="left">Description : </div></td>
<td width="82%"><? echo $row['description']; ?></td>
</tr>
<tr>
<td bgcolor="#E0E2EB"><div align="left">Item Code : </div></td>
<td><? echo $row['item_code']; ?></td>
</tr>
<tr>
<td bgcolor="#E0E2EB"><div align="left">Category : </div></td>
<td><? echo $row['category']; ?></td>
</tr>
<tr>
<td bgcolor="#E0E2EB"><div align="left">Current quantity : </div></td>
<td><? echo $row['quantity']; ?></td>
</tr>
<tr>

<td bgcolor="#E0E2EB">Required quantity : </td>
<td><input type="text" name="quantity" value="<? echo $row['quantity']; ?>">
<input type="hidden" name="id" value="<? echo $row['quantity']; ?>"></td>

</tr>



</table>


thnx in advance..
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Where do you store the shopping cart? Database? Session? Cookies?
 
0
•••
hi..sorry for late reply..i stored it at the database. can u help me pls..

Anyone..help me pls..i need the code so badly..huhuhu..
 
0
•••
The problem is, you are asking for a lot of help on a very large project. You need to be a little more specific, and then we can help you with smaller problems.

Specifically,

i still dont know how to automatically update the quantity in the stock after the user take the items and how to add the item to the cart as in a shopping cart system.

If I could answer that small question here, I will have written the entire system you :)

It only takes one line of sql to update the quantity in stock, but that's not the hard part. The hard part is knowing how that one line of sql interacts with the rest of the system.

For example, when do you remove the item from stock? You should probably only do that after payment is approved. And that is done by the billing system, whcih is tied into the shopping cart. But what happens if the person puts an item in the shopping cart, lets it sit there for three hours, and when they go to pay there are none left?

Or what happens if the user pays for the item successfully, but for some reason the system can't update the database?

So...inventory management needs to be tied into the shopping cart and the billing sytem, all of which need to be connected to some kind of error manager. It's all connected. The hard part is designing the system and make it error proof, especially because you are dealing with real people and real money.

So...how much of the database is already designed? What shopping cart/billing system are you using? This information will help guide the design and coding.

Let's start with that.
 
0
•••
cotton, I don't want to sound patandric but why not use a free, existing secure cart and either use it as it is or modify it to suit your needs, then the problems will be alot smaller and the result will come quicker.

Otherwise you would need to think of a included page that does just all the error checking like cef said, and the one line of code is easy for us to tell you, but it gets harder from then on.
 
0
•••
ok..thanks for the comments..i will brieftly explain how the system works first so that it'll be easier.
Actually, im developing a stationary inventory..there's no payment include..all i need to do is add item, edit and update ->that's for the admin to do which i have no problem in implemented the code.
Then there goes the user module. for the user, what i want to do is the user can search for the item they want to take ->done that too and then put the item in their cart in case they want to take other item ->that's the problem i want to solve right now. i want to know how to add item to the cart and update the quantity in the stock automatically after the user click submit button to take the item in the cart.

Any comments or suggestions..feel free to post..
thanks anyway..
 
0
•••
help pls..

Any inventory system free download sample that i can try?i have just 2 weeks to get this things work..my head's spinning already...aaahhhh...
 
0
•••
A couple of us posted back to your thread in the code sub-forum. Should we continue the discussion here or there (or perhaps recommend that the mods merge them here?)

Every free shopping cart out there (zen cart, oscommerce for example) has inventory management built in. You might be better off using one of those (ignoring/not implementing the payment subsystems) rather than writing something from scratch.

zen cart: http://www.zen-cart.com/
osCommerce: http://www.oscommerce.com/
 
0
•••
yes, i am thinking of doing that also..it's running out of time anyway.Thanks for your suggestion..

view.php

<td><form name="form2" method="get" action="dbView_Item.php">
<table width="80%" border="1" align="center" cellpadding="0" cellspacing="1" bordercolor="#CCCCCC">
<tr>
<td width="18%" bgcolor="#E0E2EB"><div align="left">Description : </div></td>
<td width="82%"><? echo $row['description']; ?></td>
</tr>
<tr>
<td bgcolor="#E0E2EB"><div align="left">Item Code : </div></td>
<td><? echo $row['item_code']; ?></td>
</tr>
<tr>
<td bgcolor="#E0E2EB"><div align="left">Category : </div></td>
<td><? echo $row['category']; ?></td>
</tr>
<tr>
<td bgcolor="#E0E2EB"><div align="left">Current quantity : </div></td>
<td><? echo $row['quantity']; ?></td>
</tr>
<td width="18%" bgcolor="#E0E2EB"> Required Quantity : </td>
<td width="82%">
<input name="c_qty" type="text" id="c_qty" size="10" maxlength="40"></td>
</tr>


</table>


dbView.php

<?php
session_start();

mysql_connect("localhost", "root", "") or die("no connection");
mysql_select_db("alatulis") or die("no database");
$description = $_POST['description'];
$category = $_POST['category'];
$quantity = $_POST['quantity'];
$ref = $_POST['item_code'];
if ($quantity > 0 ){
$c_qty = $_POST['c_qty'];
$co_qty = $_POST['co_qty'];

$co_qty = $quantity - $c_qty;

$query = "UPDATE stationary SET description='$description', category='$category', quantity='$co_quantity' where item_code='$item_code' ";
mysql_query($query) or die("not insert");
}
echo "<SCRIPT>alert('Data has been updated')</script>";
echo "<meta http-equiv=\"refresh\" content=\"0; URL=Home.php\">";


?>

i can't update the quantity..is there something wrong with my code? plus it says undefined index description, quantity etc..
 
0
•••
Um...I'm a little confused by both code blocks: there seem to be some inconsistencies, or else you've left some things out.

First, in the top block of code, you have

<form name="form2" method="get" action="dbView_Item.php">

But below that you have a codeblock you call "dbView.php".

Is dbView_Item.php supposed to be dbView.php?

If so, you are are submitting the form with a GET, but in dbView(_Item) you are looking at $_POST variables, which will of course be empty.

Also, in dbView(_Item), you have the line

$co_qty = $quantity - $c_qty;

But in the SQL call you have

quantity='$co_quantity'

Shouldn't that be $co_qty?

Also, I don't see where you declare/set $item_code, which is also referenced in your SQL query statement.

Let's start with all that :)
 
0
•••
PHP:
View.php

<td><form name="form1" method="post" action="dbView_Item.php">
                                <table width="80%"  border="1" align="center" cellpadding="0" cellspacing="1" bordercolor="#CCCCCC">
                                  <tr>
                                    <td width="18%" bgcolor="#E0E2EB">Description : </td>
                                    <td width="82%"><? echo $row['description']; ?></td>
									  <input type="hidden" name="id" value="<? echo $row['item_code']; ?>">
                                  <tr>
                                    <td bgcolor="#E0E2EB">Category  : </td>
                                    <td width="82%"><? echo $row['category']; ?></td>
                                  </tr>
                                  <tr>
                                    <td bgcolor="#E0E2EB">Item Code : </td>
                                   <td width="82%"><? echo $row['item_code']; ?></td>
                                      
                                  
                                  <tr>
                                    <td bgcolor="#E0E2EB">Current Quantity: </td>
                                    <td width="82%"><? echo $row['quantity']; ?></td>
                                  </tr>
                                
                                  <tr>
                                    <td bgcolor="#E0E2EB">Required Quantity  : </td>
                                    <td><input name="c_qty" type="text" id="ct_qty" size="10" maxlength="10"></td>
                                  </tr>
</table>
                                <br>
                                <table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0">
                                  <tr>
                                    <td width="67%"><div align="right">
                                        <input name="Cancel" type="button" id="Cancel" value="Cancel" onClick="history.go(-1)">
                                        <input name="Submit" type="submit" onClick="MM_validateForm('description','','R','quantity','','RisNum','item_code','','R');return document.MM_returnValue" value="Submit">
                                    </div></td>
                                  </tr>
                                </table>

PHP:
dbView_Item.php

$id = $_POST['id'];
$item_code = $_POST['item_code'];
$description = $_POST['description'];
$quantity = $_POST['quantity'];
$category = $_POST['category'];
$c_qty = $_POST['c_qty'];
$co_qty = $_POST['co_qty'];

if ( $quantity > 0 )
{
$co_qty = $quantity - $c_qty;
}

$query = "UPDATE stationary SET co_qty='$co_qty', description='$description', category='$category' where item_code='$id' ";
mysql_query($query) or die("not insert");

when i run it,it displays "not insert "and undefined index item_code, description..etc..how to fix it? The quantity failed to update..uhuhuhu..
 
Last edited:
0
•••
What exactly is echo $row['description']; printing out? (same question for all the other "echo row" statements -- category, item_code, quantity)

If the line isn't printing out someting like

Code:
<input name="description" type="text" id="description" etc. etc. etc. />

Then the form field isn't being created correctly, so the $_POST parameter in dbView_Item.php will be null and the sql call will fail.




.
 
0
•••
PHP:
$quantity = $_POST['quantity'];


$query = "UPDATE stationary SET description='$description', category='$category', quantity='$co_quantity' where item_code='$item_code' ";
mysql_query($query) or die("not insert");
}

Look at the two above statements. This have something to do with updating the quantity of the items.

Time is running out? Are you coding this for someone? Are you geting paid to get this done?

On the other hand you have a bigger problem with the script. The varibles are not cleaned properly. The way your coding it is opening yourself up to sql attacks.
 
0
•••
I'd mentioned the sql injection vulnerability as well, in another thread about this project the OP started in the code sub-forum. I figured one step at a time, but good of you to point it out here too :) Not enough people escaping their variables, wrapping their sql calls, etc.
 
0
•••
echo $row['description']; and the etc printing out the value that it holds at that time..e.g $row['description'];will show pencil...the $row['quantity']; shows the current quantity for the pencil in the stock..the only one that does not call any value is the required quantity :

<td bgcolor="#E0E2EB">Required Quantity : </td>
<td><input name="c_qty" type="text" id="ct_qty" size="10" maxlength="10"></td>

Because, here where i want to put the required quantity that the user want to take. No, im not getting paid for this.Currently, im in a training session and if i don't get this done by this month, maybe i'll fail this training.. :( Im screwed!..
 
0
•••
PHP:
mysql_query( "UPDATE `stationary` SET `co_qty` = $co_qty, `description` = '$description', category = $category WHERE`item_code` = $id LIMIT 1") or die(mysql_error());
Use this and tell us what the error is outputed.

-
 
0
•••
this is the output:

Notice: Undefined index: description in c:\program files\easyphp1-8\www\inventory\user\dbview_item.php on line 5

Notice: Undefined index: quantity in c:\program files\easyphp1-8\www\inventory\user\dbview_item.php on line 6

Notice: Undefined index: category in c:\program files\easyphp1-8\www\inventory\user\dbview_item.php on line 7

Notice: Undefined index: co_qty in c:\program files\easyphp1-8\www\inventory\user\dbview_item.php on line 9

Notice: Undefined index: item_code in c:\program files\easyphp1-8\www\inventory\user\dbview_item.php on line 11

Unknown column 'co_qty' in 'field list'

after i put isset like this: $category = $_POST['category'])? $_POST['category'] : '');

the undefined index is gone but it still shows unknown column 'co_qty'. and the qty doesnt changed.
 
0
•••
What you need to do is actually set the form fields in view.php in post #11 so that they get passed to dbView_Item.php in the $_POST variables.

I saw this in view.php from post #11:

PHP:
<input type="hidden" name="id" value="<? echo $row['item_code']; ?>">

Three problems with that line:
1. no terminating / in the <> declaration
2. the name is "id" but you're printing the value for "item_code"
3. you're missing the equivalent kind of line for all the other variables.

So you need to remove it.

You're already populating a form field correctly for c_qty, so that one's ok.

You'll need something like this in the form in order to populate the remaining $_POST variables in dbView_Item.php correctly:
PHP:
<input type="hidden" name="id" value="<? echo $row['id']; ?>" />
<input type="hidden" name="item_code" value="<? echo $row['item_code']; ?>" />
<input type="hidden" name="description" value="<? echo $row['description']; ?>" />
<input type="hidden" name="quantity" value="<? echo $row['quantity']; ?>" />
<input type="hidden" name="category" value="<? echo $row['category']; ?>" />
<input type="hidden" name="co_qty" value="<? echo $row['co_qty']; ?>" />

I don't think I left anything out or have any typos, but you're going to double check all this of course ;)







.
 
0
•••
Good find! I thought they set the item_code already in the view.php .
 
0
•••
Thank you very much!!..it settled some of my problems..im so grateful for all your helps..the codes help me much..but i think there's problems in my dbView_Item code. I think my algorithm/calculation for the quantity is wrong..when i click submit, the quantity succesfully changed but--to zero.. :|

Does anyone have better algorithm for me to use?
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back