how to do the calculation for current stock

Page 1 of 1

2 Replies - 964 Views - Last Post: 04 February 2013 - 08:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=311048&amp;s=4f7d50ab8e7809fc89a38135da4d7267&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 ygboxx

Reputation: 0
• Posts: 3
• Joined: 03-February 13

how to do the calculation for current stock

Posted 03 February 2013 - 06:25 PM

hi, im new here.

I had a problem about to calculate the current stock. How I can deduct a value from database ? I had a form that contain a value that will be deduct from database.

value from database (inStock) - value from form(prod_taken)(php)=\$result

the \$result will update the inStock in database.

The conclusion is, how I create a code for calculation?
This is my code:
```
if(isset(\$_POST['submitted'])){

\$rp=trim(\$_POST['NoRp']);
\$p=trim(\$_POST['prod_id']);
\$pn=trim(\$_POST['product']);
\$pk=trim(\$_POST['prod_taken']);

\$un=trim(\$_POST['unit']);
\$t=trim(\$_POST['terminal']);

\$errors=array();

if (empty(\$_POST['prod_id'])){
\$errors[] ='Please enter the product id';
} else {
\$p=trim(\$_POST['prod_id']);
}

if (empty(\$errors)) {

\$sql= "INSERT INTO issue ( NoRp, prod_id, product, prod_taken, unit,terminal,date) VALUES
('\$rp','\$p','\$pn','\$pk','\$un','\$t', NOW() )";
\$[email protected]_query(\$sql);
if(\$result) {

</center>';

exit();
} else {
<p class="error">  System error. We apologize for any inconvenience.</p>';

exit();
}

mysql_close();
}else {
<p class="error"> The following error(s) occurred:<br/>';
foreach(\$errors as \$msg) {
echo "- \$msg<br/>\n";
}
}
}

?>

<form action="stockform.php" method="post">

<p>Rp No: <input type="text" name="NoRp" size="20" maxlength="40" value="<?php if (isset(\$_POST['NoRp']))  echo \$_POST['NoRp']; ?>"/></p>
<p>Product id: <input type="text" name="prod_id" size="20" maxlength="40" value="<?php if (isset(\$_POST['prod_id']))  echo \$_POST['prod_id']; ?>" /></p>

<p>
<?php
\$user="root"; //user for server
\$host="localhost"; //host for server
\$database="inventory"; //database for server
\$table="product"; //table for server

mysql_connect(\$host, \$user, \$password) or die ("can't connect");
mysql_select_db(\$database) or die (mysql_error());

\$query = "SELECT prodName FROM product" ;
\$result = mysql_query(\$query);
echo'Product Name: <select name="product">';
while(\$row = mysql_fetch_assoc( \$result )) {
echo '<option value="'.\$row['prodName'].'">' . \$row['prodName'] . '</option>';
}
echo '</select>';
?></p>

<p>Total Taken: <input type="text" name="prod_taken" size="20" maxlength="40" value="<?php if (isset(\$_POST['prod_taken']))  echo \$_POST['prod_taken']; ?>"/></p>

<p>
<?php
\$user="root"; //user for server
\$host="localhost"; //host for server
\$database="inventory"; //database for server
\$table="unit"; //table for server

mysql_connect(\$host, \$user, \$password) or die ("can't connect");
mysql_select_db(\$database) or die (mysql_error());

\$query = "SELECT unitName FROM unit" ;
\$result = mysql_query(\$query);
echo'Unit: <select name="unit">';
while(\$row = mysql_fetch_assoc( \$result )) {
echo '<option value="'.\$row['unitName'].'">' . \$row['unitName'] . '</option>';
}
echo '</select>';
?></p>

<p>
<?php
\$user="root"; //user for server
\$host="localhost"; //host for server
\$database="inventory"; //database for server
\$table="terminal"; //table for server

mysql_connect(\$host, \$user, \$password) or die ("can't connect");
mysql_select_db(\$database) or die (mysql_error());

\$query = "SELECT terName FROM terminal" ;
\$result = mysql_query(\$query);
echo'Terminal: <select name="terminal">';
while(\$row = mysql_fetch_assoc( \$result )) {
echo '<option value="'.\$row['terName'].'">' . \$row['terName'] . '</option>';
}
echo '</select>';
?></p>

<br>

<p align="center"><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE"/>
</form>

</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

</body>
</html>

```

tq

Is This A Good Question/Topic? 0

Replies To: how to do the calculation for current stock

#2 modi123_1

• Suitor #2

Reputation: 13955
• Posts: 55,712
• Joined: 12-June 08

Re: how to do the calculation for current stock

Posted 04 February 2013 - 08:12 AM

Quote

How I can deduct a value from database ?

Either you load the value from the database, do the math, and update the row with the new value.. or you create a stored procedure that takes in the value you want to deduct *AND* the row you want it to happen on, and the stored procedure then updates the specific row's cell's value with the math.

Quote

The conclusion is, how I create a code for calculation?

what?

#3 CTphpnwb

• D.I.C Lover

Reputation: 3778
• Posts: 13,686
• Joined: 08-August 08

Re: how to do the calculation for current stock

Posted 04 February 2013 - 08:52 AM

I can't bear to read your code, so here's what I see wrong at a glance:
Indenting. Well, the lack of it!
You need to forget about Mysql functions and learn prepared statements. Here's a great PDO tutorial.

You shouldn't be using tables for formatting either. Read up on CSS.