INSERT WITH TWO TABLES - PHP

1

I have the student table and the payments table, I'm doing a gym system. I have listed the two tables by putting student_id in the payments table as a foreign key.

I want you to enter the data together of these two tables on the screen when enrolling student information and payment information.

The registration of the student information (student table) is done successfully, but the payment (table payments) does not insert into the bank anything ...

I have tried to do with msql_insert_id and mysqli_insert_id but says that it does not exist, let alone last_insert_id ...

CREATE.PHP

<?php
session_start();
include_once 'conexao.php';

$nome = filter_input(INPUT_POST, 'nome',FILTER_SANITIZE_SPECIAL_CHARS);
$cpf = filter_input(INPUT_POST, 'cpf', FILTER_SANITIZE_SPECIAL_CHARS);
$rg = filter_input(INPUT_POST, 'rg', FILTER_SANITIZE_SPECIAL_CHARS);
$nascimento = filter_input(INPUT_POST, 'nascimento', 
FILTER_SANITIZE_SPECIAL_CHARS);
$sexo = filter_input(INPUT_POST, 'sexo', FILTER_SANITIZE_SPECIAL_CHARS);
$fone = filter_input(INPUT_POST, 'fone', FILTER_SANITIZE_SPECIAL_CHARS);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_SPECIAL_CHARS);
$endereco = filter_input(INPUT_POST, 'endereco', 
FILTER_SANITIZE_SPECIAL_CHARS);
$bairro = filter_input(INPUT_POST, 'bairro', FILTER_SANITIZE_SPECIAL_CHARS);
$cep = filter_input(INPUT_POST, 'cep', FILTER_SANITIZE_SPECIAL_CHARS);
$estado = filter_input(INPUT_POST, 'estado',FILTER_SANITIZE_SPECIAL_CHARS);
$cidade = filter_input(INPUT_POST, 'cidade', FILTER_SANITIZE_SPECIAL_CHARS);
$situacao_aluno = filter_input(INPUT_POST, 'situacao_aluno', 
FILTER_SANITIZE_SPECIAL_CHARS);
$validade_plano = filter_input(INPUT_POST, 'validade_plano', 
FILTER_SANITIZE_SPECIAL_CHARS);
$planos = filter_input(INPUT_POST, 'planos', FILTER_SANITIZE_SPECIAL_CHARS);
$vencimento = filter_input(INPUT_POST, 'vencimento', 
FILTER_SANITIZE_SPECIAL_CHARS);
$cpf_amigo = filter_input(INPUT_POST, 'cpf_amigo', 
FILTER_SANITIZE_SPECIAL_CHARS);
$forma_pagamento = filter_input(INPUT_POST, 'forma_pagamento', 
FILTER_SANITIZE_SPECIAL_CHARS);
$data_matricula = filter_input(INPUT_POST, 'data_matricula', 
FILTER_SANITIZE_SPECIAL_CHARS);
$numero_documento = filter_input(INPUT_POST, 'numero_documento', 
FILTER_SANITIZE_SPECIAL_CHARS);
$data_documento = filter_input(INPUT_POST, 'data_documento', 
FILTER_SANITIZE_SPECIAL_CHARS);
$valor = filter_input(INPUT_POST, 'valor', FILTER_SANITIZE_SPECIAL_CHARS);

$querySelect1 = $link->query("select email from alunos");
$array_emails = [];

while($emails = $querySelect1->fetch_assoc()):
$emails_existentes = $emails ['email'];
array_push($array_emails,$emails_existentes);
endwhile;

if(in_array($email,$array_emails)):
$_SESSION['msg'] = "<p class='center red-text'>".'Já existe um aluno com 
esse email'."</p>";
    header("Location:../");
else:
 $queryInsert1 = $link->query ("insert into alunos values(default,'$nome'
'$cpf','$rg','$nascimento','$sexo','$fone','$email','$endereco','$bairro', 
'$cep','$estado','$cidade')");

$queryInsert2 = $link->query("insert into pagamentos values(default, 
$situacao_aluno','$validade_plano','$planos','$vencimento',
'$cpf_amigo','$forma_pagamento','$data_matricula',
'$numero_documento','$data_documento','$valor')");


    $affected_rows = mysqli_affected_rows($link);

    if($affected_rows > 0):
        $_SESSION['msg'] = "<p class='center green-text'>".'Cadastro 
     efetuado com sucesso!'."</br>";
        header("Location:../cadastro.php");
    endif;
   endif;
  ?>

I no longer know what to do ... Do you understand this?

    
asked by anonymous 24.09.2018 / 17:10

1 answer

1
Usa a função mysql_insert_id();

depois do insert aluno, vc usa mysql_insert_id() e grava em outra variavel.
Exemplo:
$aluno_ID = mysql_insert_id();

E depois usa essa variavel no seu insert pagamento
    
24.09.2018 / 19:09