我将我的代码从PHP,MySQL转换为sqlsrv,并且出于某种原因我的购物车'被称为不明的索引。知道为什么吗?
<?php
if ($_SERVER['REQUEST_URI'] != '/index.php?page=cart') {
$sql="SELECT * FROM table WHERE id IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
@$query= sqlsrv_query($conn, $sql);
$numrows = sqlsrv_num_rows($query);
if($numrows != 0) {
?><h1>Current Order</h1><?php while($row= sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
?>
<p> <?php echo $row['name'] ?> X <b><?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></b></p>
<?php
}
?>
答案 0 :(得分:1)
在session_start()
代码
<?php
session_start();
if ($_SERVER['REQUEST_URI'] != '/index.php?page=cart')
{
$sql="SELECT * FROM table WHERE id IN (";
foreach($_SESSION['cart'] as $id => $value)
{
$sql.=$id.",";
}
它会对你有用,也可以isset
检查$_SESSION['cart']
然后再使用它。