保存会话数据并将其发布到数据库

时间:2019-06-04 21:34:40

标签: php sql database session post

我有一个网站,您可以在其中订购一些门票。如果有人下订单,我希望将订单发送到数据库。我也想把特定的购物车中的物品发送到数据库中。查看我使用的模型:

INSERT INTO DIM_REGION (TOWN, County, District, latitude) 
SELECT p.town_city, p.county, p.district, d.latitude
FROM (
SELECT town_city, MAX(county) county, MAX(district) district
FROM DWH_PRICE_PAID_RECORDS 
GROUP BY town_city
) p
join dwh_postcodes d on p.town_city = d.town;

这是我在表单中使用的代码:

ORDERS              CART ITEMS

id*                 id
amount              order_id*
                    product_id
                    quantity 

当用户按下按钮转到<form action="index.php?page=cart" method="post" class=""> <table class=" table__nav"> <thead> <tr> <th class="p p--bold p--th th--ticket">Ticket</th> <th class="p p--bold p--th th--name">Name</th> <th class="p p--bold p--th th--quantity">Quantity</th> <th class="p p--bold p--th th--price">Price per ticket</th> <th class="p p--bold p--th th--itemtotal">Item Total</th> </tr> </thead> <tbody> <?php $total = 0; foreach($_SESSION['cart'] as $ticket) { $ticketTotal = $ticket['ticket']['price'] * $ticket['quantity']; $total += $ticketTotal; ?> <tr> <td> <?php echo $ticket['ticket']['eye_cart']; ?> </td> <td> <p class = "p--bold"><?php echo $ticket['ticket']['name']; ?></p> </td> <td> <input class = "p td--quantity" type="number" name="quantity[<?php echo $ticket['ticket']['id'];?>]" value="<?php echo $ticket['quantity'];?>" class="replace" required /> </td> <td> <p>&#8364;<?php echo $ticket['ticket']['price'];?>,-</p> </td> <td> <p>&#8364;<?php echo $ticketTotal;?>,-</p> </td> <td> <td class="remove-item"><button type="submit" class="btn remove-from-cart" name="remove" value="<?php echo $ticket['ticket']['id'];?>">&#215;</button></td> </td> </tr> </tbody> <?php } ?> </table> <div class="table__wrap"> <button type="submit" id="update-cart" class="btn btn--cart" name="action" value="update"> <img width="14" height="14" src="./assets/img/refresh.png" alt="refresh"> </button> </img> <p class="table__order--total"><span class="span--bold span--bold-no">Total:</span></p> <p class="table__order--number"><span class="span--bold span--bold-no">&#8364; <?php echo $total ?>,-</span></p> </div> <div class="table__wrap table__wrap--end"> <a class="p p__li p__li--light" href="index.php?page=register"> <button type = "submit" name ="action" value = "details" class="btn btn--big btn--big-2 btn--dark">your details -> </button></a> </div> </form> 时,我想保存一个订单,该订单的总金额+购物车物品的order_id和数量。

我正在为此使用"your details"模型,但我真的无法弄清楚。

这是我的控制器的外观:

MVC

下一个代码是我的DAO:

if ($_POST['action'] == 'details') {
  $data =  array(
    'amount' => $total,
  );
  $insertedOrder = $this->orderDAO->insertOrder($data);
  $this->set('insertedOrder', $insertedOrder);

  if (empty($insertedOrder)) {
    $errors = $this->orderDAO->validate($data);
    $this->set('errors', $errors);
    }
  }
  if ($_POST['action'] == 'details') {
  $dataB=  array(
    'order_id' => ?,
    'product_id' => $_SESSION['cart']['id']['ticket']['id'],
    'quantity' => $_SESSION['cart']['quantity'],
  );
  $insertedCartItem = $this->orderDAO->insertCartItem($data);
  $this->set('insertedCartItem', $insertedCartItem);

  if (empty($insertedCartItem)) {
    $errors = $this->orderDAO->validateB($dataB);
    $this->set('errors', $errors);
    }
  } 

我遇到的主要问题是,我不知道如何将数据从$ _SESSION变量发送到数据库。我在控制器中遇到问题。如您所见,我正在使用$ data和$ dataB发送数据,但是我认为我在这里出错了。

0 个答案:

没有答案