Let there be a PHP method which is supposed to insert a few rows into a MySQL database, each time it’s called. Unfortunately these records represent a hierarchy and I want to persist this hierarchy by adding a key for each level. An example could be looking like this:
id hier_key_1 hier_key_2 value
0 0 0 val1
1 0 0 val2
2 1 0 val3
3 1 0 val4
4 2 1 val5
5 2 1 val6
My current solution works like this:
- Insert new record in hier_key_1 table
- Select auto-incremented id of this record
- Insert new record in hier_key_2 table
- Select auto-incremented id of this record
- Insert record in table with all values and hier_key_1 and hier_key_2 as foreign keys pointing to their tables
Is there a way to simplify this, i.e. does the insert method provides any kind of return value including the keys of the inserted data? Or should I try to generate unique keys in my PHP method and check for duplicates afterwards?
Yes, there are:
PHP:
MYSQL – SQL
and so on..
greetings