Forum Moderators: coopster

Message Too Old, No Replies

PHP calculation problem.... help needed

         

sn202

5:38 pm on Dec 20, 2004 (gmt 0)

10+ Year Member



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.

PHP Code:
<?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" ); }
}
?>

Birdman

5:47 pm on Dec 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are using double-equals where they should be single:

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

Only use double-equals when testing the value of a var. Like this:

if ($price == 10){

Regards