首页 > 解决方案 > 如何解决未捕获的类型错误:PHP 中不支持的操作数类型....

问题描述

我创建了一个使用 PHP 中的 cookie 工作的表单。这就是表格的样子。 表格

当我输入价格和数量的值时,我希望它们相乘并将结果显示在“总账单价值”文本框中。每次我输入价格和数量的值时,我都希望它们相加,这就是我使用 cookie 的原因。这是我的代码。

    <html>
    <head></head>
    <body>

    <?php
        if($_SERVER["REQUEST_METHOD"] == "POST")
        {
            $result=0;
           
            if(isset($_POST["btnReset"]))
            {
                setcookie("result", $result); 
            }

            elseif(isset($_POST["btnClick"]))
            {
                if(isset($_COOKIE["result"]))
                {
                    $result=$_COOKIE["result"];
                }
                $result= $result + $_POST["Price"]*$_POST["Quantity"];
                setcookie("result", $result);
            }
            else
            {
                if(isset($_COOKIE["result"]))
                {
                    echo "something";
                }
                
    
            }
        }
    ?>

<?php 
if (isset($result)) {
    $bill = $result;
} else {
    $bill = '';
}
?>
    
        <form method="POST" >
            <ul style="list-style-type:none;">
                <li>
                    Product Name: <input type="text" name="ProductName"> <br>
                    Price: <input type="number" name="Price" value="price"> <br>
                    Quantity: <input type="number" name="Quantity" value="qty"><br>
                    <input type="submit" name="btnClick" value="Add"> 
                    <input type="submit" name="btnReset" value="Clear">
                    <input type="submit" name="btnPrint" value="Print"><br>
                    Total Bill Value: <input type="text" name="Bill" value="<?php echo $bill; ?>">                 
                </li>
            </ul>
        </form>


         
    
    
    </body>
</html>

但是每当我单击“添加”按钮时,我都会收到此错误 错误

21号线是这个

 $result= $result + $_POST["Price"]*$_POST["Quantity"];

有人知道我为什么会收到这个错误吗?

另一件事是,当我输入产品名称、价格和数量并获得总账单价值并对许多产品执行此操作并点击“打印”按钮时,我希望打印出所有产品及其总账单价值. 我该怎么做呢?

非常感谢任何帮助。提前致谢!

编辑:所以我对 $result 做了一个 var_dump 代码

这就是我得到的

var_dump($结果)

标签: phpformscookiestypeerrorsetcookie

解决方案


我要做的第一件事是将您的 PHP 代码移到开始<html>标记上方,这将有助于避免使用setcookie(例如) 的警告。

我稍微重写了您的代码,重命名了按钮和值。它已经在 PHP 5.4.45 版本中进行了测试。

<?php

// Default result value
$result = 0;

if ( !empty( $_POST ) ) {

    $cookie_key = 'result';
    $action = !empty( $_POST[ 'action' ] ) ? $_POST[ 'action' ] : false;
    $result = !empty( $_COOKIE[ $cookie_key ] ) ? (float)$_COOKIE[ $cookie_key ] : $result;


    // Add product
    if ( $action === 'add' ) {
        $price = !empty( $_POST[ 'Price' ] ) ? (float)$_POST[ 'Price' ] : 0;
        $quantity = !empty( $_POST[ 'Quantity' ] ) ? (int)$_POST[ 'Quantity' ] : 0;
        if ( $price && $quantity ) {
            $result = $result + ( $price * $quantity );
        }
    }

    // Clear result
    else if ( $action === 'clear' ) {
        $result = 0;
    }

    // Print
    else if ( $action === 'print' ) {
        // Handle print action
    }

    // Store value
    setcookie( $cookie_key, $result );
}
?>
<html>
<head></head>
<body>
    <form method="POST" >
        <ul style="list-style-type:none;">
            <li>
                Product Name: <input type="text" name="ProductName"> <br>
                Price: <input type="number" name="Price" value="price"> <br>
                Quantity: <input type="number" name="Quantity" value="qty"><br>
                <input type="submit" name="action" value="add"> 
                <input type="submit" name="action" value="clear">
                <input type="submit" name="action" value="print"><br>
                Total Bill Value: <input type="text" name="Bill" value="<?php echo $result; ?>">
            </li>
        </ul>
    </form>

</body>
</html>

这是在行动:

在此处输入图像描述


推荐阅读