-1

This is the code that i have came out after getting the data from 2 tables which are shipping infos and order. Among the data i get one of it is product_id. Which later the product_id is the primary key of the third table name product. From the third table product i would like to retrive product_title and product_price. How should i write my sql? Below shows the sql of retrieving data from 2 tables.

 <?php
 include ('classes/functions.php');

 if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$check_receipt = "select  si.shipping_name,
    si.shipping_address,
    si.shipping_contact,
    si.shipping_email,
    o.order_date,
    o.trx_id,
    o.tracking_num,
    o.quantity,
    o.product_id
   from shipping_infos si
   inner join orders o
on si.user_id = o.user_id    
where si.user_id='".$user_id."';";

        $run_receipt_checking = mysqli_query($con, $check_receipt);
        $result = array();
    while($row = mysqli_fetch_array($run_receipt_checking)){
    array_push($result,
    array(
          'shipping_name'=>$row[0],
          'shipping_address'=>$row[1],
          'shipping_contact'=>$row[2],
          'shipping_email'=>$row[3],
          'order_date'=>$row[4],
          'trx_id'=>$row[5],
          'tracking_num'=>$row[6],  
          'quantity'=>$row[7],
          'product_id'=>$row[8]       

));
}
echo json_encode(array("result"=>$result));
 }
?>
Shadow
  • 33,525
  • 10
  • 51
  • 64
Cheong Charlene
  • 275
  • 1
  • 2
  • 12

1 Answers1

-1

you can combine three tables with JOIN

Try like below

 <?php
 include ('classes/functions.php');

 if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$check_receipt = "select  si.shipping_name,
    si.shipping_address,
    si.shipping_contact,
    si.shipping_email,
    o.order_date,
    o.trx_id,
    o.tracking_num,
    o.quantity,
    o.product_id
   from shipping_infos si
   inner join orders o   
on si.user_id = o.user_id    
inner join products p on p.product_id  = o.product_id 
where si.user_id='".$user_id."';";

        $run_receipt_checking = mysqli_query($con, $check_receipt);
        $result = array();
    while($row = mysqli_fetch_array($run_receipt_checking)){
    array_push($result,
    array(
          'shipping_name'=>$row[0],
          'shipping_address'=>$row[1],
          'shipping_contact'=>$row[2],
          'shipping_email'=>$row[3],
          'order_date'=>$row[4],
          'trx_id'=>$row[5],
          'tracking_num'=>$row[6],  
          'quantity'=>$row[7],
          'product_id'=>$row[8]       

));
}
echo json_encode(array("result"=>$result));
 }
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115