<?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_fetch_array更改爲PDO等效
Fatal error: Uncaught Error: Call to undefined function mysql_query()
我知道我需要將其更改爲PDO,但我不知道該怎麼用,同時還得到我想要的totalprice更換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(HTTP或者'PDO'或'MySQLi'看一看:// PHP 。淨/)。 – Tom
'PDO''mysql_fetch_array()'是'$ conn-> fetchAll()',其中'$ conn'是連接變量。 – Tom