MySQL How to assign the value of a Foreing Key, using the value of the primary key of another table

1

I started learning programming a little bit ago and I'm doing a small desktop application. Basically it has two parts, a client's calendar (client table) and a current account for these clients (table cta_currientes) I am using MYSQL for the management of the databases: DROP DATABASE test;

CREATE DATABASE prueba;
USE prueba;

CREATE TABLE clientes (
    id_cliente INT AUTO_INCREMENT NOT NULL,
    nombre VARCHAR (50) NOT NULL,
    telefono INT NOT NULL, 
    direccion varchar (50),
    observaciones varchar (150),
    PRIMARY KEY (id_cliente)
);

CREATE TABLE cta_corriente (
    id_cta  INT AUTO_INCREMENT NOT NULL,
    id_cliente INT NOT NULL,
    debe INT,
    haber INT,
    PRIMARY KEY (id_cta),
    FOREIGN KEY (id_cliente) REFERENCES clientes(id_cliente)
);    

I have these two tables, the question is: how can I make the FK of the table cta_current be automatically assigned when I insert a client in the client table (so that when inserting a client the pk of that table, automatically assign the FK of the cta_current table) ??

Since already if I did not post correctly (try to find in Google the solution or something similar and do not succeed), I appreciate the patience to my query. Thank you very much.

    
asked by TuKe 29.10.2018 в 18:26
source

1 answer

0

Generally the id is passed by URL parameter, this means that when making a post, the php or webservice driver that receives the post receives the id from the route from which it comes.

For example. If the route of your page is:

tupagina.xl/usuario/{id?}/post

In the controller you get the id from the URL that comes from the HTTP request.

Another way, is passing the post by a hidden input, or by data from a component made with javascript.

Of all these ways, the $ request can get the values of the correct id. To verify that they are not doing XSS you can also send a validation token.

    
answered by 29.10.2018 в 19:37