2 Replies - 562 Views - Last Post: 04 February 2013 - 08:52 AM Rate Topic: -----

#1 ygboxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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() )";
$result=@mysql_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  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,262
  • 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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,589
  • 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