我有各种数量的文件和数据库表,包括artist
,album
和tracks
。
在网页上,用户可以选择艺术家,专辑,然后选择要购买的歌曲或专辑。
所需的功能是:当用户选择购买相册时,所有曲目都会添加到购物车中。
这是一个PHP代码块,其中包含购买相册的链接:
<p>?php
session_start(); <br>
$albumID=$_POST["albumID"]; <br>
echo "<p>Going to buy album $albumID</p>";
echo "<p><a href=\"shopForTracks.php\">Click here to continue</a></p>";
?></p>
我还有其他带有数据库查询等的文件。
有一个人可以通过信件获得艺术家,另一个人可以获得艺术家的专辑。然后,购物,展示篮子,显示购买,添加到篮子和结帐文件。
非常感谢任何有关此问题的帮助。
来自getTracksByAlbum.php的附加代码
?php
include ("dbConnect.php");
$albumID=$_GET["id"];
$dbQuery="select id,title from tracks where albumID='$albumID' order by trackNumber
asc";
$dbResult=mysql_query($dbQuery);
echo $albumID."\n";
echo mysql_num_rows($dbResult)."\n";
while ($dbRow=mysql_fetch_array($dbResult)) {
echo $dbRow["id"]."_".$dbRow["title"]."\n";
}
?>
showBasket.php的附加代码
<?php
if (isset($_SESSION["currentUserID"])) {
$dbQuery="select * from basket where paid='N' and userID=".$_SESSION["currentUserID"];
$dbResult=mysql_query($dbQuery);
$numTracks=mysql_num_rows($dbResult);
}
?>
<a href="login.php">Logout <?php echo $_SESSION["currentUser"]; ?></a> |
<a href="shopForTracks.php">Shop for tracks</a> |
<a href="showBasket.php">Show Basket</a> <?php echo "($numTracks)"; ?> |
<a href="checkout.php">Checkout</a> |
<a href="showMyPurchases.php">Show my purchases</a>
<hr>
<?php
$dbQuery="select tracks.title, albums.title, artists.name, basket.id ".
"from basket,tracks,albums,artists ".
"where basket.userID=".$_SESSION["currentUserID"]." ".
"and basket.paid='N' ".
"and basket.trackID=tracks.id ".
"and albums.id=tracks.albumID ".
"and artists.id=tracks.artistID";
$dbResult=mysql_query($dbQuery);
$numTracks=mysql_num_rows($dbResult);
if ($numTracks==0)
echo "<h3>Your basket is empty</h3>";
else {
?>
我不确定需要哪些其他信息,我不完全理解并且有很多其他信息。我原来是用这个 -
$query = mysql_query("SELECT song_id FROM song WHERE album = '".$_POST['albumID']."'")
$_SESSION['ID] = array();
while($album = mysql_fetch_array($query)
{
$_SESSION['basket'][] = $albums['Track_id']
}
试图解决它 - 但我真的输了:(
答案 0 :(得分:2)
根据您的最后一段代码,您需要执行与shofForTracks.php
// We get the album to add via the `albumID` GET parameter
$query = mysql_query("SELECT song_id FROM song WHERE album = '".mysql_real_escape_string($_GET['albumID'])."'")
// We add a line to the cart per track of the album. We construct the query by pieces
$insert = "INSERT INTO basket (userID, paid, trackID) VALUES ";
$template = "(" . mysql_real_escape_string($_SESSION['currentUserID']) . ", 'N', %d)";
// Add a value line for each track in the array `$tracks`
$tracks = array()
while($track = mysql_fetch_array($query)
$tracks[] = sprintf($template, $track['song_id']);
// Add the lines to the insert query
// "INSERT INTO ... VALUES (ID, 'N', 1), (ID, 'N', 3)"
$insert .= implode(", ", $tracks);
mysql_query($insert);
请注意:
$_POST
,$_GET
,...)。您现有的代码容易受到SQL injection。mysql_*
函数。切换到mysqli
或PDO。请参阅this和that以及that。GET
请求向购物车添加内容会导致安全漏洞,例如XSS