Page 1 of 1

PHP calculation problem.... help needed

Posted: Mon Dec 20, 2004 11:29 am
by sn202
Hi,

I have a page which the user enters "$studentid", "$productid" and "$quantity". This then runs the PHP script (shown below) which updates the product "$quantity" ($sql2), updates student "$balance" and enters all required info into table "transaction". All this works correctly except the "$price" isn't being calculated meaning that "$order_total" is being shown as just the "$quantiy" instead of "$quantity * $price".

Obviously something wrong with how i'm assigning the variable "$price". Any help with this will be much appreciated!

Regards Simon.

Code: Select all

<?php	$self =		$_SERVER['PHP_SELF'];
		$studentid =	$_POST['studentid'];
		$productid =	$_POST['productid'];
		$quantity =	$_POST['quantity'];
#connect to MYSQL
$conn = @mysql_connect( "linuxproj", "***", "*****" )
				or die( "could not connect" );
#select the specified database
$rs = @mysql_select_db ( "db_sn202", $conn )
			or die( "could not select database" );


#runs slected PHP function
#show stock
#create the sql query
if( $studentid and $productid and $quantity ) #ensure values exist
{
$sql="select price from products where productid='$productid'";
 #exercute the query
$rs = mysql_query( $sql, $conn )
	  or die( mysql_error() );

$num=mysql_numrows($rs);
$i=0;
while ($i < $num) {
$price==mysql_result($rs,$i,"price");
$i++;
}
$order_total == $price * $quantity;

$sql2="update products set quantity = quantity-'$quantity' where productid = '$productid'";
#exercute the query
$rs2 = mysql_query( $sql2, $conn )
	  or die( mysql_error() );
$sql3="update students set balance = balance-'$order_total' where studentid = '$studentid'";
#exercute the query
$rs3 = mysql_query( $sql3, $conn )
	  or die( mysql_error() );
$sql4="insert into transaction (studentid, productid, type, value) values ('$studentid', '$productid', 'out', '$order_total')";
#exercute the query
$rs4 = mysql_query( $sql4, $conn )
	  or die( mysql_error() );
if($rs4) { echo( "transaction complete:$studentid $productid $quantity $order_total" ); }
}
?>

Posted: Mon Dec 20, 2004 11:48 am
by gurjit
i would rewrite this section like this to begin with, which will get you the correct order total

Code: Select all

<?php
#ensure values exist
if (($studentid != '')  && ($productid != '') && ($quantity != '')) 
{
$sql="select price from products where productid='$productid'"; 
#excute the query
$rs = mysql_query( $sql, $conn )      or die( mysql_error() );
$get = mysql_fetch_assoc($rs);


$price = $get['price'];

$order_total = $price * $quantity;
}
?>

Posted: Mon Dec 20, 2004 11:52 am
by sn202
Thanks a lot mate, that's sorted it!!!!
Simon.