我收到此错误:
致命错误:不支持的操作数类型 在C:\ xampp \ htdocs \ process.php上 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 :(得分:2)
尝试使用类型转换(你可以使用(浮点)或任何你需要的,我使用(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;
答案 1 :(得分:1)
您将返回GET数组而不是结果。
function calculate_topping_cost() {
$topping = 1;
return $_GET['topping'];
}
试试这个:
function calculate_topping_cost() {
$topping = 1;
return $topping;
}
答案 2 :(得分:1)
$_GET["topping"]
可能包含不是数字的内容。
你可以强行施放它:
function calculate_topping_cost() {
$topping = 1;
return (int) $_GET['topping'];
}
如果0
不是数字,则会返回topping
。
但是您可能希望明确地捕获该条件,或修复代码 - 该函数没有多大意义,这可能只是一个错误。