【问题标题】:Shopping cart with PHP and SQL使用 PHP 和 SQL 的购物车
【发布时间】:2014-04-30 17:19:30
【问题描述】:

我有不同数量的文件和数据库表,包括artistalbumtracks

在网页上,用户可以选择艺术家、专辑,然后选择要购买的歌曲或专辑。

所需的功能是:当用户选择购买专辑时,所有曲目都被添加到购物车中。

这是一个 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']
  }

尝试解决 - 但我真的迷路了:(

【问题讨论】:

  • 我们需要更多信息才能有效地提供帮助。特别是,我们需要知道购物车如何存储物品以及如何将它们添加到其中。请发布与此相关的代码。

标签: php mysql sql cart


【解决方案1】:

根据你最后一段代码,你需要在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。请参阅thisthatthat
  • 上面的代码不安全原样。使用简单的 HTTP GET 请求将东西添加到您的购物车可能会导致安全漏洞,例如 XSS
  • 抱歉,从您的代码看来,您似乎还没有准备好自己编写一个真实的(即真钱)购物网站(无论如何)。您仍然需要了解对 Web 交易至关重要的安全性。为了你自己,如果你对代码的安全性没有绝对的信心,不要让人们用他们的钱来信任你。但是,嘿,我只是说。如果您将这些东西编码为练习,那么有机会熟悉这些概念:)

【讨论】:

  • 非常感谢!是的,这是任务的一部分,几乎没有给出任何指示。
猜你喜欢
  • 1970-01-01
  • 2020-11-04
  • 2017-06-17
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多