2

如果这很简单,我还在学习,很抱歉......我想要实现的是一个在线商店结帐系统(不要担心它不会被使用它只是为了我自己的学习,所以任何草率的代码都不会影响一个可怜的企业主但是任何草率代码的皮卡总是值得赞赏的)。

所以我让客户选择产品,然后将它们存储在数组中并显示在篮子中。然后我想将用户带到结帐页面,以便他们可以单击“订单”,然后将这些项目连同他们的用户名一起更新到数据库中的“订单”表中。

我仍然在处理数组(可能是我处于哪个学习阶段的标识符)所以我不确定当我有几个时如何将这些项目(基本上只是 ISBN 号和价格)从我的购物篮带到结帐页面不同的数组。我要做的是展示我如何创建每个数组以及如何在篮子中显示它,希望这将是您可能需要的一切!非常感谢您的阅读,任何帮助将不胜感激。我希望这个问题也可以帮助处于我这个阶段的其他人。

创建数组=

产品.php

echo "<a href='addtolist.php?bookname=" . $bookname . "&bookauthor=" . 
     $bookauthor . "&bookpub=" . $bookpub . "&bookprice=" . $bookprice . 
     "&bookisbn=" . $bookisbn . "'>Add to basket</a>";

添加列表.php

<?php
 session_start();
 if ( !isset($_SESSION['username']) )
{
header("Location:index.php");
exit();
}

$bookarray = array();
$bookarray['bookname'] = $_GET['bookname'];
$bookarray['bookauthor'] = $_GET['bookauthor'];
$bookarray['bookpub'] = $_GET['bookpub'];
$bookarray['bookisbn'] = $_GET['bookisbn'];
$bookarray['bookprice'] = $_GET['bookprice'];

$found = false;

if (isset($_SESSION['list']))
{
  foreach ($_SESSION['list'] as $key => $another)
  {
    if ($_SESSION['list'][$key]['bookisbn'] == $_GET['bookisbn'])
        {
          $found = true;
          break;
    }
  }
}

if ($found == false)
{
  $_SESSION['list'] [] = $bookarray;
}

header('Location: ' . $_SERVER['HTTP_REFERER']);

exit();
?>

显示列表.php

<?php

if (!isset($_SESSION['list']))
{
echo "No list members selected";
}
else
{
?>
<div id="bket2">

<table>
<tr>
<td class='toprow'>Title</td>
<td class='toprow'>Author</td>
<td class='toprow'>Publisher</td>
<td class='toprow'>ISBN</td>
<td class='toprow'>Price</td>
</tr>

<?php

$totalprice=0;

foreach ($_SESSION['list'] as $key => $another)
{
?>
<tr>
        <td><?php echo $_SESSION['list'][$key]['bookname']?></td>
        <td><?php echo $_SESSION['list'][$key]['bookauthor']?></td>
    <td><?php echo $_SESSION['list'][$key]['bookpub']?></td>
    <td><?php echo $_SESSION['list'][$key]['bookisbn']?></td>
    <td><?php echo "&#163;".$_SESSION['list'][$key]['bookprice']?></td>
    <td><a href="removefromlist.php?bookisbn= <?php echo $_SESSION['list'][$key]['bookisbn']?> &location= <?php echo
    $_SERVER['PHP_SELF']?> ">[-]</a></td>
    </tr>
    <?$totalprice += $_SESSION['list'][$key]['bookprice'];?>
    <?php
    }
    }
    ?>
    </table>
    <div><h4>Total Price = &#163;<?echo $totalprice;?></h4></div>

</div>

然后在我的篮子里我放

<?php require "displaylist.php" ?

多谢你们

4

2 回答 2

1

这是一个可以帮助您入门的示例。

<?php

$dbh = new PDO('mysql:host=localhost;dbname=mysql', 'username', 'password');
$stmt = $dbh->prepare('insert into orders (isbn, price) values (?, ?)');

if (isset($_SESSION['list']))
{
  foreach ($_SESSION['list'] as $item)
  {
    $stmt->execute(array($item['bookisbn'], $item['bookprice']));
  }
}
?>
于 2012-04-12T22:28:55.360 回答
0

不完全确定您要做什么,但我假设您想将存储在 $_SESSION['list'] 中的账面价值插入 mysql 中的“订单”表中。这是我的看法(假设您将“用户名”存储在某处的会话变量中):

<?php
session_start();

//connect to db
$con=mysql_connect("localhost","username","password");

if(!$con){
    die("Could not connect to the database: " .mysql_error());
};

mysql_select_db("db_name",$con);

//loop through each book in 'list' and add values to mysql table 'order'
for($i=0; $i<count($_SESSION['list']);$i++){

    $query="INSERT INTO `orders` 
                ('username',
                'isbn',
                'price',
                'timestamp') 
                VALUES 
                ('".mysql_real_escape_string($_SESSION['username'])."',
                '".mysql_real_escape_string($_SESSION['list'][$i]['bookisbn'])."',
                '".mysql_real_escape_string($_SESSION['list'][$i]['bookprice'])."',
                CURDATE()
                )";

    $result=mysql_query($query);
}

mysql_close();

    ?>
于 2012-04-12T22:52:21.727 回答