如何定义索引?为什么我看到错误:遇到非数字值?

时间:2019-03-27 19:22:17

标签: php html

我正在尝试使用表索引的名称。但是,在启动网页时,我看到以下错误:

  

第73、75、76、77、79和80行的未定义索引。

我在这里做错什么以及如何纠正它。

有问题的未定义索引是:

id, pname, price and image. 

索引:

index

我的代码:

  <div class="container" style="width: 65%">
        <h2>Ticket Cart</h2>
        <?php
        $query = $handle->query('SELECT * FROM product ORDER BY id ASC');

            while ($row = $query->fetchAll(PDO::FETCH_ASSOC)){


        }
        ?>
        <div class="col-md-3">
            <form method="post" action="cart.php?action=add&id=<?php echo $row ["id"]?>"> // line 73
                <div class="product">
                    <img src="<?php echo $row["image"]; ?>" class="img-responsive"> // line 75
                    <h5 class = "text-info"><?php $row["pname"]; ?> </h5> //line 76
                    <h5 class="text-danger"><?php $row["price"]; ?></h5> //line77
                    <input type="text" name="quantity" class="form-control" value="1">
                    <input type="hidden" name="hidden_name" value="<?php echo $row["pname"]; ?>"> // line 79
                    <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>"> // line 80
                    <input type="submit" name="add" style="margin-top: 5px;" class="btn btn-success"
                           value="Add to Cart">

                </div>

            </form>
    </div>

下面是我收到错误的第154和160行:非数值:

$ <?php echo number_format($value["item_quantity"] * $value["product_price"], 2); ?></td> // line 154

 $total = $total + ($value["item_quantity"] * $value["product_price"]); // line 160

2 个答案:

答案 0 :(得分:0)

为了克服我遇到的第一个错误,这是我需要在文件开头添加以下代码的未定义索引:

if (isset($_POST["add"])){
    if (isset($_SESSION["cart"])){
        $item_array_id = array_column($_SESSION["cart"],"product_id");
        if (!in_array($_GET["id"],$item_array_id)){
            $count = count($_SESSION["cart"]);
            $item_array = array(
                'product_id' => $_GET["id"],
                'item_name' => $_POST["hidden_name"],
                'product_price' => $_POST["hidden_price"],
                'item_quantity' => $_POST["quantity"],
            );
            $_SESSION["cart"][$count] = $item_array;
            echo '<script>window.location="Cart.php"</script>';
        }else{
            echo '<script>alert("Product is already Added to Cart")</script>';
            echo '<script>window.location="Cart.php"</script>';
        }
    }else{
        $item_array = array(
            'product_id' => $_GET["id"],
            'item_name' => $_POST["hidden_name"],
            'product_price' => $_POST["hidden_price"],
            'item_quantity' => $_POST["quantity"],
        );
        $_SESSION["cart"][0] = $item_array;
    }
}

答案 1 :(得分:-1)

代码的主要问题是while循环在您认为您将输出接收到的行的输出之前结束。 index没有对数据库索引的任何引用,仅表示您正在使用不存在像“ pname”或“ price”之类的索引访问对象$row

相应地更改代码:

<?php
$query = $handle->query('SELECT * FROM product ORDER BY id ASC');

while ($row = $query->fetch(PDO::FETCH_ASSOC))
{

?>
<div class="col-md-3">
    <form method="post" action="cart.php?action=add&id=<?php echo $row["id"]?>"> // line 73
        <div class="product">
            <img src="<?php echo $row["image"]; ?>" class="img-responsive"> // line 75
            <h5 class = "text-info"><?php $row["pname"]; ?> </h5> //line 76
            <h5 class="text-danger"><?php $row["price"]; ?></h5> //line77
            <input type="text" name="quantity" class="form-control" value="1">
            <input type="hidden" name="hidden_name" value="<?php echo $row["pname"]; ?>"> // line 79
            <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>"> // line 80
            <input type="submit" name="add" style="margin-top: 5px;" class="btn btn-success"
                   value="Add to Cart">

        </div>

    </form>
</div>
<?php
}
?>