从数组中取出项目并将它们放入数据库中

时间:2012-04-12 22:03:21

标签: php arrays

我仍然在学习,如果这很简单就很抱歉......我想要实现的是一个在线商店结账系统(不要担心它不会被用于它只是为了我自己的学习所以任何草率的代码都不会影响穷人企业主,但任何关于草率代码的拾取器总是受到赞赏。

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

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

创建array =

products.php

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

addtolist.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();
?>

displaylist.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" ?

谢谢你们

2 个答案:

答案 0 :(得分: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']));
  }
}
?>

答案 1 :(得分:0)

我不确定你要做什么,但我想你想把存储在$ _SESSION ['list']中的图书值插入到mysql的'orders'表中。这是我对它的看法(假设你的'用户名'存储在某个会话变量中):

<?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();

    ?>