php - 以 html 形式更新一个依赖于另一个表的表值

标签 php mysql

Order 和 Line_items 两个表相互关联,因为 order_id 放在行项目中,

现在首先填充订单表,然后根据订单表的 ID 填充 lineitems 表。 一个 isset() 中的所有 heppening 有两个函数,一个在订单表中添加值,第二个函数获取订单 ID,然后填充 lineitems 函数。

问题是第一个订单项在 Html 表单的 order_id 值属性中的订单 ID 是“”。 (我很难解释这个问题)。

这是我的 Php isset() 函数。

 if(isset($_POST['check_out'])){
    $user_id = $_POST['user_id'];
    $shipping_address = $_POST['shipping_address'];
    $billing_address = $_POST['billing_address'];
    $shipping_method_id = $_POST['shipping_method_id'];
    $grand_total = $_POST['grand_total'];
$order->addOrder($user_id,$shipping_address,$billing_address,$shipping_method_id,$grand_total);

$order_id = $_POST['latestorder'];
$product_id = $_POST['product_id'];
$product_price = $_POST['product_price'];
$product_quantity = $_POST['product_quantity'];
$product_total = $_POST['product_total'];
if (empty($order_id)) {
        $order_id=$order->latestOrderRecord();
        $order_id=+1;
    }


$id= count($product_id);

for ($i=0; $i <$id ; $i++) { 

    $line_items->insertInLineItems($order_id,$product_id[$i],$product_price[$i],$product_quantity[$i],$product_total[$i]);
    // $shoping_cart->

    header('location:../cart.php');

    }


}

html 表单

<form method="post" action="n/GetPostData.php" class="woocommerce-shipping-calculator">
  <td data-title="Shipping">
    Flat Rate: 
    <span class="amount">$300.00</span> 
    <p><a data-toggle="collapse" aria-controls="calculator" href="#calculator" aria-expanded="false" class="shipping-calculator-button">Calculate Shipping</a></p>
    <div id="calculator" class="shipping-calculator-form collapse">
      <p id="calc_shipping_country_field" class="form-row form-row-wide">
        <select rel="calc_shipping_state" class="country_to_state" id="calc_shipping_country" name="shipping_method_id">
          <option>Select Shipment Method</option>
          <?php $shipping_method=$shipment_method->allShipmentMethod(); 
            while ($row=mysqli_fetch_assoc($shipping_method)) { ?>
          <option value="<?php echo $row['id'] ?>"><?php echo $row['name']; ?></option>
          <?php } ?>
        </select>
      </p>
      <p id="calc_shipping_country_field" class="form-row form-row-wide">
        <input type="text" id="calc_shipping_postcode" name="shipping_address" placeholder="Shipping Address"  class="input-text">
      </p>
      <p id="calc_shipping_state_field" class="form-row form-row-wide validate-required">
        <input type="text" id="calc_shipping_postcode" name="billing_address" placeholder="Billing Address"  class="input-text">
      </p>
    </div>
  </td>
  </tr>
  <tr class="order-total">
    <th>Total</th>
    <td data-title="Total">
      <strong><span class="amount">$<?php echo $total; ?></span></strong> 
      <input type="hidden" name="grand_total" value="<?php echo $total ?>">
      <input type="hidden" name="user_id" value="<?php echo $id ?>">
      <?php $latestrecord=$order->latestOrderRecord(); ?>
      <input type="hidden" name="latestorder" value="<?php echo $latestrecord ?>">
      <?php $total=0; $all_cart_products=$shoping_cart->allShopingcartValue($id); 
        while ($row=mysqli_fetch_assoc($all_cart_products)) { ?>
      <input type="hidden" name="product_id[]" value="<?php echo $row['product_id']; ?>">
      <input type="hidden" name="product_quantity[]" value="<?php echo $row['quantity']; ?>">
      <input type="hidden" name="product_price[]" value="<?php echo $row['price']; ?>">
      <input type="hidden" name="product_total[]" value="<?php echo $row['total']; ?>">
      <?php } ?>
      <p><button type="submit" class="button" name="check_out" type="submit">Chcek Out</button></p>
    </td>
  </tr>
</form>

第一次当订单表为空时,name="latestorder" 显示“”(无值)。

这是latestOrderRecord()函数定义

public function latestOrderRecord()
{

    $query="SELECT MAX(id) AS LatestRecord FROM orders";

    $conn=$this->Connection();

    $result = mysqli_query($conn,$query);

    $num_rows = mysqli_num_rows($result);
    if ($num_rows>0) {
        while ($row=mysqli_fetch_assoc($result)) {
            $latestrecord=$row['LatestRecord'];
         }
    }
    return $latestrecord;
}

Line items table

Orders table Picture

最佳答案

问题是,您在 for() 循环中使用了 header() 重定向:

for ($i=0; $i <$id ; $i++) { 
    header('location:../cart.php'); // that is the issue 
}

你必须在for循环中使用一个成功变量,然后你可以在循环外使用它进行重定向,比如:

提示:

if($success){
    header('location:../cart.php');
    exit(); // always use exit() after header() otherwise script execution will not stop
}

编辑:

根据您的评论,您在 lineitem 表中获得订单 ID 0,因为您正在插入 $_POST['latestorder'] 值为 0,因为您不这样做有最新的身份证。

您需要插入此查询的最后插入的 id:

$order->addOrder($user_id,$shipping_address,$billing_address,$shipping_method_id,$grand_total);

addOrder() 方法返回最后插入的 id,并将其存储到一个新变量中,并在您的 lineitem 表中使用此变量,例如:

$lastInsertID = $order->addOrder($user_id,$shipping_address,$billing_address,$shipping_method_id,$grand_total);

并且,这里 $line_items->insertInLineItems($order_id,$product_id[$i],$product_price[$i],$product_quantity[$i],$product_total[$i]);$order_id 替换为 $lastInsertID

确保 addOrder() 方法返回最后插入的 ID。

关于php - 以 html 形式更新一个依赖于另一个表的表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56988996/

相关文章:

php - 如果两个类位于不同的文件夹中,是否可以有两个同名的类?

PHP MySQL 插入/更新查询返回 true 但有错误

mysql - 使用 SQL 将两个表连接到一个表中

php - 如何在 Symfony 2 的 Form (Type) 类中获取实体存储库的实例?

php - 我如何查看下拉列表中的一个选项产生的数据?

php - 为 wordpress 主题创建 Bootstrap 导航栏

php - 如何使用PDO bindParam将点数据插入mysql?

javascript - 后退按钮是通过 JS 还是 PHP 实现的?

MySQL 更新查询不适用于不同的列

php - 使用 LEFT OUTER JOIN 连接多个表