获取致命错误

问题描述:

我收到此错误:获取致命错误

Fatal error: Unsupported operand types in C:\xampp\htdocs\process.php on line 59("$total = calculate_size_cost() + calculate_topping_cost() + calculate_delivery_cost();")

<?php 
    $name = $_GET['name']; 
    $phone = $_GET['phone']; 
    $address = $_GET['address']; 
    $size = $_GET['size']; 
    $topping = $_GET['topping']; 
    $delivery = $_GET['deliverytype']; 
    $comment=$_GET['comments']; 

    mysql_connect ("localhost", "root", "") or die ('Error: ' . mysql_error()); 
    mysql_select_db ("pizza"); 
    $query ="INSERT INTO orders (name, phone, address, size, topping, delivery, comments) VALUES ('".$name."', '".$phone."', '".$address."','".$size."','".$topping."','".$delivery."','".$comment."')"; 



    $total = 0; 
    $total = calculate_size_cost() + calculate_topping_cost() + calculate_delivery_cost(); 

    echo "Dear $name your {$_GET["size"]} pizza has been ordered."; 
    echo "Your Total is $ $total"; 
    echo "\n\n\nYour Toppings: {$_GET["topping"]}"; 
    echo "\nYour Comments: {$_GET["comments"]}"; 
    echo "Your Delivery Type:{$_GET["deliverytype"]}"; 

    function calculate_size_cost() { 
     $size = 0; 
     if ($_GET['size'] == "Small"){ 
      $size+=5; 

     } 
     else if ($_GET['size'] == "Medium"){ 
      $size+=10; 

     } 
     else if ($_GET['size'] == "Large"){ 
      $size+=15; 
     } 
     return $size; 


    } 

    function calculate_topping_cost() { 
     $topping = 1; 
     return $_GET['topping']; 
    } 

    function calculate_delivery_cost() { 
     $delivery_cost = 0; 
     if ($_GET['deliverytype'] == "delivery") { 
      $delivery_cost += 5; 
     } 
     return $delivery_cost; 
    } 
    ?> 
+0

'$ _GET ['topping']'包含什么? – 2011-04-18 13:14:39

+1

我假设$ _GET ['topping'];不是一个数字:)。 – 2011-04-18 13:14:47

+0

它包含$ topping的值。 – Ankur 2011-04-18 13:16:02

尝试类型转换(u可以使用(浮动)或任何ü需要,使用IM(INT)

$size =(int) $_GET['size']; 
$topping =(int)$_GET['topping']; 
$delivery = (int)$_GET['deliverytype']; 

也做同样的每个功能的返回值,如

return (int)$size;等等......关于

@Ankur一个建议:改变插入的语法(我喜欢这个)

$query =" INSERT INTO orders SET 
       name='".$name."', 
       phone='".$phone."', 
       address='".$address."', 
       size='".$size."', 
       topping='".$topping."', 
       delivery='".$delivery."', 
       comments='".$comment."')"; 

然后写

$done=mysql_query($query); 
echo $done; 
+0

感谢您的帮助。但仍然没有将数据插入到数据库中。数据库显示MySQL返回了一个空的结果集。 – Ankur 2011-04-18 13:26:15

+0

表示致命错误消失。现在只有mysql问题。我对吗? – diEcho 2011-04-18 13:27:26

+0

是的,MySQL的问题。它不会将值插入表中。 – Ankur 2011-04-18 13:28:50

您返回GET数组,而不是你的结果。

function calculate_topping_cost() { 
    $topping = 1; 
    return $_GET['topping']; 
} 

试试这个:

function calculate_topping_cost() { 
    $topping = 1; 
    return $topping; 
} 

$_GET["topping"]可能包含的东西是不是数字。 你可能会迫使它转换:

function calculate_topping_cost() { 
     $topping = 1; 
     return (int) $_GET['topping']; 
    } 

这将返回0如果topping不是一个数字。

但是,您可能希望明确地捕获该条件或修复代码 - 该函数没有多大意义,这可能只是一个错误。

+0

修复了代码,但有一件事,不是显示浇头,而是显示“数组”字样。 – Ankur 2011-04-18 13:48:44