Insert data Database Related tables

0

I'm having trouble inserting data that has related tables.

$sqlinsert2 = "INSERT INTO tb_detalhe_trabalhador VALUES(0,NULL,NULL,'".$MedicaValidade."',..

Currently the data that is NULL is the fields related to the Table.

tb_detail_worker:

'id' int(11) NOT NULL AUTO_INCREMENT,
'tb_trabalhador_id' int(11) DEFAULT NULL,
'tb_funcoes_id' int(11) DEFAULT NULL,
'MedicaValidade' date DEFAULT NULL,
'MedicaAnexo' longblob,
 (...)
    
asked by anonymous 24.02.2014 / 13:24

3 answers

2

I understand that you are not able to insert the data in the table tb_detail_worker because it has two foreign keys (tb_worker_id, tb_funcoes_id) so it will not be possible because even with default null it must be linked to the reference tables, or it can be by cause of the value zero (0) as the id of the worker_better that you are inserting that is not possible, in this case, because it is an autoincrement field, it is not necessary to pass any value.

    
24.02.2014 / 13:41
2

There is a resource called mysql_insert_id() that will return the key for the insert, assign this resource to a variable, and then re-insert it into the related table.

    
24.02.2014 / 13:38
2

You must have tb_trabalhador_id already registered in the related table before making this query or it will give error. Also, the value of tb_trabalhador_id must be exactly the same as in the related table.

  

    
24.02.2014 / 13:42