I cannot insert in related tables

Asked

Viewed 479 times

0

I am in trouble. I normally insert in the table "equipment" of my code and normally populate the mysql BD.

However, the table "dell" is empty, even passing the reference of the ID of the table "equipment". By phpmyadmin is all working normally, if I insert by there works, but I have to do via PHP on mysqli.

"equipment" is independent and "dell" is a dependent table with Fk_item which is auto_increment

Helping!!!

<?php
//tabela dell
$fabricante=$_POST['fabricante'];
$modelo=$_POST['modelo'];
$tipo=$_POST['tipo'];
$data=date("Y/m/d");
$url=$_POST['url'];
$serie=$_POST['serie'];
$proprietario=$_POST['proprietario'];
$origem=$_POST['origem'];
$venc_garantia=date("Y/m/d");
$tipo_instalacao=$_POST['tipo_instalacao'];
$configuracao=$_POST['configuracao'];
$uname_a=$_POST['uname_a'];
$instalacao=$_POST['instalacao'];
$status=$_POST['status'];
$servicos=$_POST['servicos'];

//tabela equipamento
$descricao=$_POST['descricao'];
$quantidade=$_POST['quantidade'];
$tombamento=$_POST['tombamento'];
$local=$_POST['local'];

//tabela equipamento
$query = "INSERT INTO equipamento (item, tipo, descricao, quantidade, tombamento, local, status)
VALUES(NULL, '$tipo', '$descricao', '$quantidade', '$tombamento', '$local','$status')";

$mysqli->query($query);
printf ("Registro de id %d.\n", $mysqli->insert_id);
//tabela dell
$query2 = "INSERT INTO dell (id_item, fabricante, modelo, tipo, data, url, serie, proprietario, origem, venc_garantia, tipo_instalacao,
configuracao, uname_a, instalacao, servicos, tombamento) VALUES(LAST_INSERT_ID(), '$fabricante', '$modelo', 
'$tipo', '$data', '$url', '$serie', '$proprietario', '$origem', '$venc_garantia', '$tipo_instalacao', '$configuracao', 
'$uname_a', '$instalacao', '$status', '$servicos', '$tombamento')";
$mysqli->query($query2);
printf("Novo Registro foi inserido com sucesso\n"); 
printf ("Registro de id %d.\n", $mysqli->insert_id);
?>

2 answers

0

In the INSERT INTO dell... realized that you are entering 17 values in 16 fields. The number of fields must be exactly equal to the number of values passed in the VALUES. This may be the reason for the problem.

I believe you have forgotten about the STATUS field (notice that it is in VALUES but is not in the list of fields in the table).

Try it like this:

$query2 = "INSERT INTO dell (id_item, fabricante, modelo, tipo, data, url, serie, proprietario, origem, venc_garantia, tipo_instalacao,
configuracao, uname_a, instalacao, status, servicos, tombamento) VALUES(LAST_INSERT_ID(), '$fabricante', '$modelo', 
'$tipo', '$data', '$url', '$serie', '$proprietario', '$origem', '$venc_garantia', '$tipo_instalacao', '$configuracao', 
'$uname_a', '$instalacao', '$status', '$servicos', '$tombamento')";
  • I will test Aki and Jaja return the result.

  • gave yes. I placed positive in his comment, but I think the site did not register. Vlw even the help and support. They really helped me. Can you suggest me some good program so I don’t make these mistakes? I’m using Notepad++ to make these codes :/

0


$query2 = "INSERT INTO dell (id_item, fabricante, modelo, tipo, data, url, serie,      proprietario, origem, venc_garantia, tipo_instalacao,
configuracao, uname_a, instalacao, servicos, tombamento) VALUES(LAST_INSERT_ID(),     '$fabricante', '$modelo', 
'$tipo', '$data', '$url', '$serie', '$proprietario', '$origem', '$venc_garantia',     '$tipo_instalacao', '$configuracao', 
'$uname_a', '$instalacao', '$status', '$servicos', '$tombamento')";

In this your second QUERY in the fields to insert you have a field that is not mapped which is STATUS.

Check this which is probably the reason.

Replace with this one:

$query2 = "INSERT INTO dell (id_item, fabricante, modelo, tipo, data, url, serie,      proprietario, origem, venc_garantia, tipo_instalacao,
configuracao, uname_a, instalacao, status, servicos, tombamento) VALUES(LAST_INSERT_ID(),     '$fabricante', '$modelo', 
'$tipo', '$data', '$url', '$serie', '$proprietario', '$origem', '$venc_garantia',     '$tipo_instalacao', '$configuracao', 
'$uname_a', '$instalacao', '$status', '$servicos', '$tombamento')";
  • I will test Aki and Jaja return the result.

  • And then it worked out, man?

  • gave yes. I placed positive in his comment, but I think the site did not register. Vlw even the help and support. They really helped me. Can you suggest me some good program so I don’t make these mistakes? I’m using Notepad++ to make these codes :/

  • Press the arrow up next to my comment and mark as the right answer if you can! The recommended is that you do this in some IDE...you can download either Netbeans (For PHP) or Eclipse (For PHP). The two will work smoothly with your project!

  • I put the arrow already. I can not give a "up" because I do not have 15 of reputation. Thank you for the support aew Dante. Hugs!

  • Nothing expensive, always arrange! o

Show 1 more comment

Browser other questions tagged

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