带有PDO样式连接的php购物车页面中的未定义索引
我试图使用带有PDO样式连接的会话创建购物车,但我遇到了错误 “注意:未定义索引:名称在”“注意:未定义索引:价格在” 我很确定这存在于我的数据库表中,下面是代码带有PDO样式连接的php购物车页面中的未定义索引,php,mysql,pdo,Php,Mysql,Pdo,我试图使用带有PDO样式连接的会话创建购物车,但我遇到了错误 “注意:未定义索引:名称在”“注意:未定义索引:价格在” 我很确定这存在于我的数据库表中,下面是代码 <?php $stmt = $conn->prepare('SELECT * from tbl_product'); $stmt->execute(); if($stmt->fetchColumn() > 0) { while($row = $stmt
<?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
}
在pdo语句中使用,它将返回一个数组。因此,您需要通过$row[0]['name']访问,或者使用
foreach($row as $v){
$v['name'];
}
问题在于函数fetchAll
一次返回所有行。您需要通过mysqli
的fetch\u 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
}
这是最后的工作代码,谢谢,再次感谢大家
<?php
$stmt = $conn->prepare('SELECT * from tbl_product');
$stmt->execute();
if($stmt->fetchColumn() > 0)
{
$all_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($all_rows as $row)
{
?>
<div class="col-md-4">
<form method="post" action="../ppuyakul/cata_main.php?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 print_r($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
}
}
?>
打印$row
通过打印($row)
&显示输出您确定您的表中有一列名称
。tbl\u产品
?@AgamBanga谢谢我现在就试试,@hassan yes非常确定已经在那里了=(@AgamBanga Array([0]=>Array([id]=>2[name]=>HP Notebook[image]=>2.jpg[price]=>299.00[1]=>Array([id]=>3[name]=>Panasonic T44 Lite[image]=>3.jpg[price]=>125.00))$Array([0]=>Array([id]=>2[name]=>HP Notebook[image]=>2.jpg[price]=>299.00[1]=>Array([id]=>3[name]=>Panasonic T44 Lite[image]=>3.jpg[price]=>125.00))或者通过使用fetchAll并使用foreach对其进行迭代,$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);foreach($rows…
@hassan,是的,这也是正确的,更新了答案。谢谢:)很乐意帮助:)