Pass the primary key value of a table to a foreign table of another PHP table

Asked

Viewed 89 times

0

I’ve used MAX(column name_name), last_insertid, and nothing works, the foreign key of the second table keeps getting value 0.

public function cadastrar_teste($nr_ficha_teste, $cod_refugo_teste, 
   $qtd_refugo, $codigo_profissional){

    $conexao = Database::getConnection();

    $sql = "INSERT INTO teste (nr_ficha_teste, cod_refugo_teste, qtd_refugo, cod_usuario_profissional)
            VALUES ('$nr_ficha_teste', '$cod_refugo_teste', '$qtd_refugo', '$codigo_profissional');";

    $conexao->exec($sql);
}

 require "../Models/Teste.php";
 require "../Models/Profissional.php";
 require_once "../Models/Cabecalho.php";

 $ficha = new Cabecalho();
 $nr_ficha = $ficha->busca_ficha();
 $cod_refugo_teste = $_POST['cod_refugo_teste'];
 $qtd_refugo = $_POST['qtd_refugo'];
 $codigo = $_POST['cod_usuario_profissional'];
 $valoresrefugos = explode(",",$cod_refugo_teste);
 $valoresqtds = explode(",",$qtd_refugo);

 $unir = 'INSERT INTO teste (cod_refugo_teste, qtd_refugo) VALUES (';
 for ($i = 0; $i < count($valoresrefugos); $i++) {
     if ($i == count($valoresrefugos) - 1) {
    $unir .= "'" . $valoresrefugos[$i] . "','" . $valoresqtds[$i] . "')";
}
else {
    $unir .= "'" . $valoresrefugos[$i] . "','" . $valoresqtds[$i] . "'), (";
  }
 }



 if (is_numeric($codigo)){          
$teste = new Teste();
$teste->cadastrar_teste($nr_ficha, $cod_refugo_teste, $qtd_refugo, $codigo);
header("location:../?pgs=modal_cadastro_teste");
    }else{
        echo "Erro!";
    }

.

public function busca_ficha(){
$conexao = Database::getConnection();

$select="SELECT MAX(nr_ficha) FROM cab_teste";

  $busca = $conexao->query($select);
  $nr_ficha = $busca->fetchAll(PDO::FETCH_ASSOC);

  return $nr_ficha;
 }

nr_ficha is the primary key of table 1. inserir a descrição da imagem aqui

nr_ficha_test is the foreign key of table 2. inserir a descrição da imagem aqui inserir a descrição da imagem aqui

AN ADDENDUM: the value was receiving zero value because it was not really foreign key, BUT I still can’t pass any value to nr_ficha_test. Now that I connected the two tables, the FK in table 2 gets NULL value.

  • Put the function busca_ficha() question. Probably the value coming from there is 0

  • Ready, I put.

  • Silvia, does it really need to be via PHP? It’s much simpler and fast right in SQL.

  • Do you say just make a query? Well, I would need PHP because this is a web project.

  • What you want to do is change 2 tables. It could not be direct in the bank, with update? Why create everything in PHP to do this? It will be a routine or is a "fix"?

  • I don’t want to update, I want to pass the value from one key to another. The foreign key in table 2 has no value.

  • still needs help?

  • Yeah, I couldn’t solve the problem. I get an error like this: Fatal error: Uncaught Pdoexception: SQLSTATE[23000]: Integrity Constraint Violation: 1452 Cannot add or update a Child Row: a Foreign key Cont fails

Show 3 more comments

1 answer

0

Guys, I was able to solve it. The problem was not in the database, but in php. When I passed the value, I transformed the variable into an array. To make the array work, simply IN MY CASE, give the column a nickname and pass this nickname as parameter. It looks like this:

public function busca_ficha(){
 $conexao = Database::getConnection();

 $select="SELECT MAX(nr_ficha) AS ULTIMO FROM cab_teste";

 $busca = $conexao->query($select);
 $nr_ficha = $busca->fetchAll(PDO::FETCH_ASSOC);

 return $nr_ficha[0]['ULTIMO'];
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.