Doubt with php and mysql

Asked

Viewed 103 times

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.

  • 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.

  • 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.

  • it’s auto increment I think it’s not necessary not to !

  • id_user is related to id in the user table

  • 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.

  • I’ll put it here and test

  • and step what parameter to it in query ? I leave empty ?

  • 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.

  • 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

  • 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 !

Show 6 more comments

1 answer

1

This Foreign Keys ID is a relationship, not an automatic introduction of data to maintain the relationship. You’re the one who has to maintain the integrity of the relationship when you do the data intro.

You first have to get the user ID when you store data in the user table, and then use that ID to store the data in the imc table.

Example:

$sql = mysqli_query("INSERT INTO usuario(nome) VALUES('$nome')");
$last_id = mysqli_insert_id();
$sql = mysqli_query("INSERT INTO imc(usuario_id, peso,altura,imc) VALUES($last_id, '$peso','$altura','$imc')");

Browser other questions tagged

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