我創建了簡單的購物車應用程序。在這些我想添加產品到購物車中,從那個購物車我只選擇一個產品購買。我在使用id時感到困惑。任何人都可以幫我解決這個問題嗎?以spicific id顯示數據庫中的數據
這裏是添加到購物車中的代碼:
<?php
Include('connect.php');
$productId=$_POST['productId'];
$productName=$_POST['productName'];
$productPrice=$_POST['productPrice'];
$quantity=$_POST['quantity'];
$sql=mysql_query("INSERT INTO products (productId,productName,productPrice,quantity)
VALUES ('$productId','$productName','$productPrice','$quantity')") or die(mysql_error());
if($sql){
$_GET['productId']=$productId;
header('Location:buy.php');
}
else
{
echo"Failed to add cart !";
}
?>
這裏是獲取產品和顯示的代碼:
<?php
include('connect.php');
$sql="SELECT productName FROM products WHERE productId=" . $productId . "";
$result = mysql_query($sql);
$myArray = array();
echo "<table style='width:50%;'>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Quantity</th>
</tr>";
$index = 0;
while($row = mysql_fetch_assoc($result))
{
$myArray[$index] = $row;
$index++;
echo "<tr>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['productPrice'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "</tr>";
}
?>
從哪裏 「$的productId」 將在第二個代碼值? – jophab