INSERT in two different tables - MySql

4

I have two tables in Mysql:

  • Alunos: id, nome, idade, email, senha.

  • login: id, email, senha.

When I insert to write the information in the alunos table, can I make another insert to write the email and password in login ?

    
asked by anonymous 03.10.2015 / 21:04

2 answers

2

It probably did not make it so clear, but I solved it like this:

I get the data from a form:

$nome = $_POST['nome'];

$idade = $_POST['idade'];

$email = $_POST['email'];

$senha = $_POST['senha'];

$sql1 = "INSERT INTO aluno (nome, idade, email, senha) values ('$nome', '$idade', '$email', '$senha')";

$sql2 = "INSERT INTO login (email, senha) values ('$email', '$senha')";

mysqli_query($conexao, sql1) or die ("Erro"); 

mysqli_query($conexao, sql2) or die ("Erro");

echo 'Gravado com sucesso';

So you have recorded the data received from the form in both tables. :)

    
03.10.2015 / 22:16
5

Yes, just create a STORED PROCEDURE to insert into the two tables;

The code below creates PROCEDURE:

DELIMITER $$ 

    CREATE PROCEDURE Insere(IN aNome VARCHAR(30), IN aIdade INT, IN aEmail VARCHAR(30), IN aSenha VARCHAR(30)) 
    BEGIN 
        INSERT INTO Alunos ( nome, idade, email, senha) VALUES (aNome, aIdade, aEmail, aSenha);
        INSERT INTO Login ( email, senha) VALUES (aEmail, aSenha);
    END $$ 
DELIMITER ;

After that, just run PROCEDURE with the command CALL Insere

Ex:

CALL Insere ('Fulano', 22, '[email protected]', 'senha');

After that the user will be inserted into the two tables.

    
03.10.2015 / 21:21