带有PDO样式连接的php购物车页面中的未定义索引(Undefined index in php shopping cart page with PDO style connection)

编程入门 行业动态 更新时间:2024-10-26 15:24:38
带有PDO样式连接的php购物车页面中的未定义索引(Undefined index in php shopping cart page with PDO style connection)

我尝试使用带有PDO样式连接的SESSION创建购物车,但我面临错误

“注意:未定义索引:名称在” “通知:未定义索引:价格在”

我很确定我的数据库表中存在,这是代码

<?php $stmt = $conn->prepare('SELECT * from tbl_product'); $stmt->execute(); if($stmt->fetchColumn() > 0) { while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) { ?> <div class="col-md-4"> <form method="post" action="../ppuyakul/cata_main?action=add&id=<?php echo $row["id"]; ?>"> <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center"> <img src="<?php echo $row["image"]; ?>" class="img-responsive" /><br /> <h4 class="text-info"><?php echo $row["name"] ?></h4> <h4 class="text-danger">$ <?php echo $row["price"]; ?></h4> <input type="text" name="quantity" class="form-control" value="1" /> <input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" /> <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" /> <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" /> </div> </form> </div> <?php }

在此先感谢,我真的不知道如何解决这个问题=(

I trying to create a shopping cart using SESSION with PDO style connection, but I'm facing error

"Notice: Undefined index: name in" "Notice: Undefined index: price in"

I'm pretty sure that exists in my database table, here is the code

<?php $stmt = $conn->prepare('SELECT * from tbl_product'); $stmt->execute(); if($stmt->fetchColumn() > 0) { while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) { ?> <div class="col-md-4"> <form method="post" action="../ppuyakul/cata_main?action=add&id=<?php echo $row["id"]; ?>"> <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center"> <img src="<?php echo $row["image"]; ?>" class="img-responsive" /><br /> <h4 class="text-info"><?php echo $row["name"] ?></h4> <h4 class="text-danger">$ <?php echo $row["price"]; ?></h4> <input type="text" name="quantity" class="form-control" value="1" /> <input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" /> <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" /> <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" /> </div> </form> </div> <?php }

Thanks in advance, i really don't know how to solve this issue =(

最满意答案

问题在于fetchAll函数,它一次返回所有行。 您需要逐个获取行,这可以通过mysqli fetch_row方法完成。 你应该改变它

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

while($row = $stmt->fetch_row(PDO::FETCH_ASSOC))

或者 ,您也可以使用fetchAll但是您需要将结果存储在变量中,然后循环遍历该变量,如下所示

$all_rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($all_rows as $row) { //do the html part & anything }

The issue is with the function fetchAll which returns all the rows at once. You need to fetch the row one by one which can be done via fetch_row method of mysqli. You should change it from

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

to

while($row = $stmt->fetch_row(PDO::FETCH_ASSOC))

Alternatively, You can also use fetchAll but then you need to store the results in a variable & then loop over that variable like below

$all_rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($all_rows as $row) { //do the html part & anything }

更多推荐

本文发布于:2023-08-03 08:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:购物车   样式   索引   页面   未定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!