SQL / PHP Calculator


SQL / PHP Calculator

我试图用一个人给出的输入制作一个小计算器,将这些答案与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");
}

我建议您使用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. 在greenfeeplatinum缺少一美元
  2. 在以mysql_query的形式运行query之后,您必须以的形式获取值

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

      选择*FROM计算器

mysql_query在成功执行查询后返回一个资源,而不是一个值,你需要获取它。阅读此处

所以替换所有像下面这样的:

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

带有

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

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

正如上述评论中所提到的,您在greenfeeplatinum上缺少$

*不要使用mysql_,因为它们已被弃用