【发布时间】:2016-01-21 15:20:34
【问题描述】:
<?php
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while ($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
$totalprice+=$subtotal;
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td>
<td><?php echo $row['price'] ?>$</td>
<td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>$</td>
</tr>
<?php
}
?>
<tr>
<td>Total Price: <?php echo $totalprice ?></td>
</tr>
这是我目前使用的代码。 我得到的明显错误是:
致命错误:未捕获错误:调用未定义函数 mysql_query()
我知道我需要将其更改为 PDO,但我不确定用什么替换 mysql_fetch_array 同时仍然获得我想要的总价格。
改成这样后:
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$stmt = $db->prepare($sql);
$stmt->execute();
$totalprice=0;
在获得正确总价的同时,我不确定如何继续
【问题讨论】:
-
mysql_*功能已被删除,请查看php.net 上的PDO或MySQLi。 -
与
PDOmysql_fetch_array()是$conn->fetchAll()其中$conn是连接变量。