Receive form in 2 different tables

-1

I have this code to receive data in two different tables. But only the first table is receiving data. The second remains blank. Can this code work this way?

if(($query=mysql_query($sqlinsert)) and ($query2=mysql_query($sqlinsert2))){
$sqlinsert = "INSERT INTO tb_funcoes VALUES(0,'"....)";     
$sqlinsert2 = "INSERT INTO tb_detalhe_trabalhador VALUES(0,NULL,NULL,'o."',....)";

}

When the error occurs, it returns the following message:

Duplicate entry '0' for key 'PRIMARY'
    
asked by anonymous 07.02.2014 / 19:00

1 answer

1

The problem occurs because a 0 id already exists as the primary key in your table, so it does not allow you to enter another equal, because primary keys can not be duplicated, to solve you would have to do the following:

if(($query=mysql_query($sqlinsert)) and ($query2=mysql_query($sqlinsert2))){
  $sqlmax = "select max(nomedocampoID) from tb_funcoes";
  $sqlmax2 = "select max(nomedocampoID) from tb_detalhe_trabalhador";
  $sqlinsert = "INSERT INTO tb_funcoes VALUES(".$sqlmax.",'"....)";     
  $sqlinsert2 = "INSERT INTO tb_detalhe_trabalhador VALUES(".$sqlmax2.",NULL,NULL,'o."',....)";
}

It would be nothing more than to get the last ID (primary key) that exists in your tables, so that no ID is duplicated and always auto incrementing.

Note: do not forget to get the name of the field that contains the ID (primary key) and replace with my "fieldname" that I put above.

    
12.02.2014 / 15:19