SQL / PHP计算器

时间:2013-12-05 08:57:01

标签: php mysql forms

我试图用一个人给出的输入制作一个小计算器,将这些答案与php中声明的一些变量相加并相加。但我无法让它正常工作。

这个脚本有什么问题?从表单写入数据库部分正常工作。但是当它指向输出屏幕时,计算并不真正起作用。

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");
}

3 个答案:

答案 0 :(得分:1)

我建议你使用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
}

所以你只需要一个Select而不是那么多;)

答案 1 :(得分:0)

mysql_query在成功执行查询后返回资源而不是值,您需要获取它。Read here

所以替换以下所有内容:

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

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

OR

更好地使用 Igoel 提到的方法,这是更优化的方式。

同样如上述评论所述,您在 greenfeeplatinum

中缺少 $

* 不要使用mysql _ ,因为它们已被弃用。

答案 2 :(得分:0)

脚本中的错误:

  1. 在greenfeeplatinum错过$ $
  2. 将查询作为mysql_query运行后,您必须将值提取为

    while($row = mysql_fetch_array($nineholes))
    {
        $nine_hole = $row['nineholes'];
    }
    
    1. 您可以在单个查询中获取所有列,例如

      SELECT * FROM calculator