0
Could someone help me out here? I’m starting in php now, I created a database in mysql(phpmyadimin) and related the primary key of one table as a foreign key in another. When I run the php script with the idea of saving in the database not saved, but if I remove the relationship between the tables, saved. What you can is this?
Follows the code
$sql = mysqli_query("INSERT INTO usuario(nome) VALUES('$nome')");
$sql = mysqli_query("INSERT INTO imc(peso,altura,imc) VALUES('$peso','$altura','$imc')");
Below structure of tables.
`Estrutura da tabela `imc`
--
CREATE TABLE IF NOT EXISTS `imc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`peso` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`altura` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`imc` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`id_usuario` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id_usuario` (`id_usuario`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
Estrutura da tabela `usuario`
--
CREATE TABLE IF NOT EXISTS `usuario` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
--
-- Extraindo dados da tabela `usuario`
--
INSERT INTO `usuario` (`id`, `nome`) VALUES
(1, 'shs'),
(2, 'sem nome'),
(3, 'ertt');
--
-- Constraints for dumped tables
--
--
-- Limitadores para a tabela `imc`
--
ALTER TABLE `imc`
ADD CONSTRAINT `relacionamento` FOREIGN KEY (`id_usuario`) REFERENCES `usuario` (`id`);
Which primary field of which table is a foreign key, and in which other table? Explain better, the problem is unclear.
– user28595
table looks like this: user who has id as primary and name; and the second table is imc which has primary key id, weight, height, imc, and id_user as foreign key.
– Samuel Henrique
Why aren’t you informing the user id in the query? And you didn’t tell exactly what error is occurring. Click [Edit] and add more information to the question.
– user28595
it’s auto increment I think it’s not necessary not to !
– Samuel Henrique
id_user is related to id in the user table
– Samuel Henrique
If id_user is a foreign key in the imc table, why are you not entering it in the imc table query? Maybe this is the reason for the error. Add how are the two tables in the question.
– user28595
I’ll put it here and test
– Samuel Henrique
and step what parameter to it in query ? I leave empty ?
– Samuel Henrique
Samuel, add the schema of the tables in the question by clicking [Edit], not knowing how the fields of the two tables is difficult to help.
– user28595
I see no reason to separate this information into two tables, unless it’s just for study. See this answer => http://answall.com/a/89884/91
– rray
Yes it is only for study! using the $last_id = mysqli_insert_id() increment; it worked like I said I’m starting in php, my agr mission is to select a user in a<option> form and display on another screen the same data !
– Samuel Henrique