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.
nr_ficha_test is the foreign key of table 2.
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– Andrei Coelho
Ready, I put.
– Silvia Luize
Silvia, does it really need to be via PHP? It’s much simpler and fast right in SQL.
– rbz
Do you say just make a query? Well, I would need PHP because this is a web project.
– Silvia Luize
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"?– rbz
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.
– Silvia Luize
still needs help?
– Luís Almeida
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
– Silvia Luize