0
我需要一些幫助。 我即將創建一個小型電子商店,我想爲每個用戶添加購物歷史記錄。顯示購買歷史
我有這個,但它不像我想要的那樣工作。
$sql="SELECT * FROM transactions INNER JOIN cds ON transactions.cdCode=cds.code WHERE usrId='$userId' ORDER BY transactionId";
$result=mysqli_query($conn,$sql);
if (mysqli_num_rows($result)==0)
echo "No purchases";
echo "<table>";
while($row=mysqli_fetch_array($result))
{
echo "<tr><td>".$row['transactionId']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['cdQuantity']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['tDate']."</td>";
echo "<td>".$row['status']."</td></tr>";
}
echo "</table>";
SQL文件:
CREATE TABLE transactions (
transactionId varchar(255) NOT NULL,
usrId INT NOT NULL,
cdCode varchar(35),
cdQuantity int,
tDate text,
status text,
PRIMARY KEY(transactionId,usrId,cdCode),
FOREIGN KEY(usrId) REFERENCES users(userId),
FOREIGN KEY(cdCode) REFERENCES cds(code)
);
它打印出這樣的事情:
> purchaseId cdName quantity price date status
> 1e963a1d CdName 1 price 2015-12-12:14:08 Pending
> 1e963a1d CdName 1 price 2015-12-12:14:08 Pending
> 1e963a1d CdName 1 price 2015-12-12:14:08 Pending
> 1e963a1d CdName 1 price 2015-12-12:14:08 Pending
> 1f2b8183 CdName 1 price 2015-12-12:14:20 Pending
> 1f2b8183 CdName 1 price 2015-12-12:14:20 Pending
> 1f2b8183 CdName 1 price 2015-12-12:14:20 Pending
我想組由purchaseId不同表中的採購,以便計算的總成本每一次購物。
我很困惑... cdCode或cdName現有查詢? – Vixed