# how to do the calculation for current stock

Page 1 of 1

## 2 Replies - 825 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=f5f1cfe007e325e7eba735263ab511d6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ygboxx

• New D.I.C Head

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]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */_query(\$sql);
if(\$result) {

echo'<center><h1 id="mainhead">Thank you!<h1>
</center>';

echo "<center><a href=homeAdmin.php>BACK</a></center>";

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

exit();
}

mysql_close();
}else {
echo '<h1 id="mainhead"> Error!</h1>
<p class="error"> The following error(s) occurred:<br/>';
foreach(\$errors as \$msg) {
echo "- \$msg<br/>\n";
}
echo '</p><p> Please try again.</p><p><br/></p>';
}
}

?>

Please fill the form below
<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
\$username=""; //user for server
\$password=""; //password 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
\$username=""; //user for server
\$password=""; //password 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
\$username=""; //user for server
\$password=""; //password 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: 11617
• Posts: 45,796
• 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?
Was This Post Helpful? 0

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,351
• 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.
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}