简体   繁体   中英

Undefined index in php shopping cart page with PDO style connection

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 =(

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
}

You use fetchAll in you pdo statement, which will return an array. So you need to access by $row[0]['name'], or use

foreach($row as $v){
  $v['name'];
}

Here is the final working code, Thanks, all you guys once again really appreciated

<?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  
                     }  
                }  
                ?>   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM