Ali Raza
Ali Raza

Reputation: 489

update one table values in html form that are dependent on another table

Two tables Order and Line_items are Relate with each other as order_id is placed in line items,

now first populate the order table and then lineitems table on the base of order table's id. All of the heppening in one isset() has two functions one add values in orders table and second function take order id and then populate the lineitems function.

Problems is for the very first lineitems the order id is "" in Html form's value attribute of order_id. (very hard for me to explain this problem).

Here is my Php isset() function.

 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

<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>

for the very first time when order table is empty the name="latestorder" show "" (no value).

Here is latestOrderRecord() function definition

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

Upvotes: 1

Views: 559

Answers (1)

devpro
devpro

Reputation: 16117

Issue is that, you are using header() redirection inside your for() loop:

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

You must need to use a variable for success in for loop, then you can use it outside the loop for redirection, something like:

HINT:

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

Edit:

As per your comment, you are getting order id 0 in lineitem table, because you are inserting $_POST['latestorder'] value which is 0 because you dont have latest ID.

you need to insert the last inserted id of this query:

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

return last inserted id from addOrder() method, and store it into a new variable and use this variable in your lineitem table something like:

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

and, here $line_items->insertInLineItems($order_id,$product_id[$i],$product_price[$i],$product_quantity[$i],$product_total[$i]); replace $order_id with $lastInsertID

Make sure, method addOrder() returns last inserted ID.

Upvotes: 1

Related Questions