user3069332
user3069332

Reputation: 15

SQL / PHP Calculator

Im trying to make a little calculator with input given by a person, multiplying and adding these answers with some variables declared in php. But I can't get it to work properly.

What is wrong with this script? The writing to database part from the form works correctly. But when it directs to the output screen, the calculations don't really work.

mysql_connect("localhost" , "root" , "DM3") or die (MySQL_error());
mysql_select_db("calculator");

$order = "INSERT INTO calculator (nineholes , eightteenholes , hcp , club , academy , locker , rainflex ) VALUES ('$_POST[nineholes]','$_POST[eightteenholes]','$_POST[hcp]','$_POST[club]','$_POST[academy]','$_POST[locker]','$_POST[rainflex]') " ;
$result = mysql_query($order);  

//*Deze waarden kun je vrij veranderen
$brons = 44.00 ;
$zilver = 129.00 ;
$goud = 265.00 ;
$platinum = 599.00 ;

$greenfeebrons = 25.00 ;
$greenfeezilver = 17.50 ;
$greenfeegoud = 12.50 ;
greenfeeplatinum = 5.00 ;

$hcpx = 25.00 ;
$clubx = 65.00 ;
$academyx = 65.00 ;
$lockerx = 85.00 ;
$rainflexx = 45.00 ;

$allinx = 0.00 ;
$allinplatinumx = 0.00 ;

//*Deze waarden kun je niet veranderen

$nineholes = mysql_query('SELECT nineholes FROM calculator') ;
$eightteenholes = mysql_query('SELECT eightteenholes FROM calculator') ;
$hcp = mysql_query('SELECT hcp FROM calculator') ;
$club = mysql_query('SELECT club FROM calculator') ;
$academy = mysql_query('SELECT academy FROM calculator') ;
$locker = mysql_query('SELECT locker FROM calculator') ;
$rainflex = mysql_query('SELECT rainflex FROM calculator') ;
$allin = '0' ;
$allinplatinum = '0' ;


// Total
$bronstotaal =      $brons + (($nineholes / 4) * $greenfeebrons ) + (($eightteenholes / 5) * $greenfeebrons ) + (( $hcp / 6 ) * $hcpx ) +  (( $club / 7 ) * $clubx )  + (( $academy / 8 ) * $academyx) + (( $locker / 9 ) * $lockerx) + (( $rainflex / 10 ) * $rainflexx) + $allin + $allinplatinum;
$zilvertotaal =     $zilver + (($nineholes / 4)  * $greenfeezilver ) + (($eightteenholes / 5) * $greenfeezilver ) + (( $hcp / 6 ) * $hcpx ) +  (( $club / 7 ) * $clubx )  + (( $academy / 8 ) * $academyx) + (( $locker / 9 ) * $lockerx) + (( $rainflex / 10 ) * $rainflexx) + $allin + $allinplatinum;
$goudtotaal =       $goud + (($nineholes / 4)  * $greenfeegoud ) + (($eightteenholes / 5) * $greenfeegoud ) + (( $hcp / 6 ) * $hcpx ) +  (( $club / 7 ) * $clubx )  + (( $academy / 8 ) * $academyx) + (( $locker / 9 ) * $lockerx) + (( $rainflex / 10 ) * $rainflexx) + $allin + $allinplatinum;
$platinumtotaal =   $platinum + (($nineholes / 4)  * $greenfeeplatinum ) + (($eightteenholes / 5) * $greenfeeplatinum ) + (( $hcp / 6 ) * $hcpx ) +  (( $club / 7 ) * $clubx )  + (( $academy / 8 ) * $academyx) + (( $locker / 9 ) * $lockerx) + (( $rainflex / 10 ) * $rainflexx) + $allin + $allinplatinum;

if($result)
{
    echo ("<br> <u><b>Totaal:</b></u> <br>") ;
    echo ($bronstotaal) . "<br>" ;
    echo ($zilvertotaal) . "<br>" ;
    echo ($goudtotaal) . "<br>" ;
    echo ($platinumtotaal) . "<br>" ;
} 
else
{
    echo("<br>U heeft niet alles goed ingevuld");
}

Upvotes: 0

Views: 915

Answers (3)

R R
R R

Reputation: 2956

mysql_query returns a resource after succesful execution of the query not a value,u need to fetch it.Read here

so replace all like the below:

$nineholes = mysql_query('SELECT nineholes FROM calculator') ;

with

$nineholes = mysql_result(mysql_query('SELECT nineholes FROM calculator'),0) ;

OR

better use the method which Igoel mentioned,that is more optimized way.

also as mentioned in the above comments you are missing a $ at greenfeeplatinum

*Dont use mysql_ as they are deprecated.

Upvotes: 0

Prem Baranwal
Prem Baranwal

Reputation: 369

Errors in your script:

  1. Missing a $ at greenfeeplatinum
  2. After running query as mysql_query, you have to fetch the values as

    while($row = mysql_fetch_array($nineholes))
    {
        $nine_hole = $row['nineholes'];
    }
    
    1. You can fetch all columns in a single query like

      SELECT * FROM calculator

Upvotes: 0

Jacob A.
Jacob A.

Reputation: 270

i recommended you to use mysqli btw:

$result = mysql_query('SELECT * FROM calculator') ;

if (!$result) {
    die('Invalid query: ' . mysql_error());
}


while ($row = mysql_fetch_assoc($result)) {
    $nineholes = $row['nineholes'];
    $eightteenholes = $row['eightteenholes'];
    $hcp =  $row['hcp'];
    $club = $row['club'];
    //.
    //.
    //.
    //and so on
}

so you only need one Select and not so many ;)

Upvotes: 1

Related Questions