Inserting to header and details problem

Need some help please,This is the result of my query, I want to insert this to my header and detail table, and I want my detail table to get the header_id so that
I can reference to my header.

    [
       {"cust_id":1,"dev_id":"09997898789","reading_value":5,"readdate":"2015-09-11 03:35:19"},
       {"cust_id":1,"dev_id":"09997898789","reading_value":4,"readdate":"2015-09-11 03:35:29"},
       {"cust_id":1,"dev_id":"09997898789","reading_value":4,"readdate":"2015-09-11 03:35:39"},
       {"cust_id":2,"dev_id":"01235666666","reading_value":3,"readdate":"2015-09-11 03:38:19"},
       {"cust_id":2,"dev_id":"01235666666","reading_value":3,"readdate":"2015-09-11 03:38:30"}
       
    ]

so my tables data would look like this

Header_table

id  cust_id     totalread     datebill

1   1           13            2015-09-13

2   2           6             2015-09-13 

DETAIL_TABLE

id    devid         reading_value     readdate                   headerid

1    09997898789    5                 2015-09-11 03:35:19              1
2    09997898789    4                 2015-09-11 03:35:29              1
3    09997898789    4                 2015-09-11 03:35:39              1
4    01235666666    3                 2015-09-11 03:38:19              2
5    01235666666    3                 2015-09-11 03:38:30              2

Thank you in advance.

@fretburner @megazoid ,

I am trying to create an invoices so that I can send bill to my customers base on 30days they consumed.but I am confuse how to insert the records fetched by 30 days, to the header and details,and the details table will contain the header id so that I can get reference on them to my header. I am confuse how will I do this.or do you have some other suggestion ?

Thank you in advance.

The first post has been updated at @jemz request.

Is this what you are looking for?

$j = '[
       {"cust_id":1,"dev_id":"09997898789","reading_value":5,"readdate":"2015-09-11 03:35:19"},
       {"cust_id":1,"dev_id":"09997898789","reading_value":4,"readdate":"2015-09-11 03:35:29"},
       {"cust_id":1,"dev_id":"09997898789","reading_value":4,"readdate":"2015-09-11 03:35:39"},
       {"cust_id":2,"dev_id":"01235666666","reading_value":3,"readdate":"2015-09-11 03:38:19"},
       {"cust_id":2,"dev_id":"01235666666","reading_value":3,"readdate":"2015-09-11 03:38:30"}
    ]';
	
$jd = json_decode($j, true);

echo'<pre>'; print_r($jd); echo '</pre>';

Thanks for the reply, Nope, my problem is how can I get the header id and insert it to my detail table.that is my problem.

This is how I did first I query and get the result,
then I insert it to my header table, then I insert it to my detail, but my header_id in detail table is null, and I want to grab the header_id and insert it to detail “header_id”

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.