Question:
Is there a way to get the value of client.client_id
into a PHP variable to then use it as the value of phone.client_id
?
Context:
I have a PostgreSQL database to which I communicate using PHP.
I have two separate tables in the database:
client (client_id[PK], client_name)
and
phone (phone_id[PK], client_id[FK], phone_number)
Both client_id
and phone_id
are create as bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807
.
Since the ids are GENERATED ALWAYS AS IDENTITY
I don't need to to specify them when inserting data into the tables in PHP:
$query = "INSERT INTO public.client (client_name) VALUES('" . $clientName . "')";
Now to finally get to my question:
Whenever I insert a client
I also want to insert a phone
, the phone
table needs to hold a reference to the client
to which it belongs, and it does so by having a client_id
foreign key.
If I was just creating the id in PHP I could use that value in both INSERT
statements and end up with the correct behaviour.
Is there a way to get the value of client.client_id
into a PHP variable to then use it as the value of phone.client_id
?