请帮我完成购物车页面。我一直在收到错误,我不明白为什么,我已经尝试了我所知道的一切
//目标是我的最后一个篮子,并使用现在支付功能结账。请帮忙
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style/style.css" rel="stylesheet" type="text/css"></head>
<body>
<div id="products-wrapper">
<h1>View Cart</h1>
<div class="view-cart">
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<form method="post" action="paypal-express-checkout/process.php">';
echo '<ul>';
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm)
{
$product_code = $cart_itm["code"];
$results = $mysqli->query("SELECT product_name,product_description, price FROM products WHERE product_code='$product_code' LIMIT 1");
if( !$results)
die($mysqli->error);
$queryResult = array();
while ($obj = $results->fetch_object())
{
$queryResult[] = $obj->product_name;
}
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
echo '<div class="p-price">'.$currency.$obj['0']->price.'</div>';
echo '<div class="product-info">';
echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div>'.$obj->product_desc.'</div>';}
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
echo '<input type="hidden" name="item_price['.$cart_items.']" value="'.$obj->price.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
$cart_items ++;
}
echo '</ul>';
echo '<input type="submit" value="Pay Now" />';
echo '</form>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong></span>';
?>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
我可以看到一些错误。 在while循环中,您将在名为$ obj的temp var中逐个获取所有结果。
while ($obj = $results->fetch_object())
{
$queryResult[] = $obj->product_name;
}
在循环内部,您可以访问$ obj的所有属性。例如,您可以获得product_name,这没关系。 但是在循环的最后一次迭代之后,$ obj将始终为null,正如您在http://php.net/manual/es/mysqli-result.fetch-object.php中看到的那样 当然,在任何时候,$ obj都不是数组,所以:
为此,当你尝试
时echo '<div class="p-price">'.$currency.$obj['0']->price.'</div>';
您正在尝试获取不是数组的数组的第一项,因此它将失败。 并在
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
或
echo '<input type="hidden" name="item_price['.$cart_items.']" value="'.$obj->price.'" />';
您将始终访问null对象,因此它也将失败。
解决方案是更改while循环,因此它包含以下与数据库中一行相关的所有代码。
//keep your code until here
while ($obj = $results->fetch_object())
{
// $queryResult[] = $obj->product_name; //probably you dont need this
// } your loop ends here, but it shouldnt
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
//you dont access $obj[0], but $obj
echo '<div class="p-price">'.$currency.$obj->price.'</div>';
echo '<div class="product-info">';
echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div>'.$obj->product_desc.'</div>';}
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
echo '<input type="hidden" name="item_price['.$cart_items.']" value="'.$obj->price.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
$cart_items ++;
}
}//this is the new end of your loop
echo '</ul>';
//from here, your code remains the same
有了这个,您将解决您报告的问题,但可能您必须仔细检查您的代码,因为它可能包含其他一些错误。