INSERT WITH TWO TABLES - PHP

Asked

Viewed 220 times

1

I have the table students and the table payments, I am doing a gym system. I linked the two tables by placing the id_students in the payments table as foreign key.

I want when registering student information and payment information, appear on the screen the data together of these two tables.

The registration of the student information (table students) is performed successfully, but the payment (table payments) does not enter in the bank anything...

I’ve tried to do with msql_insert_id and mysqli_insert_id but says that this does not exist, let alone the 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 ... Someone knows this ?

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
  • Hello...I’ve done this, but it doesn’t work ... brings me this error : Fatal error: Uncaught Error: Call to Undefined Function mysql_insert_id() in C: xampp htdocs Horus banco_de_data create.php:48 Stack trace: #0 {main} thrown in C: xampp htdocs Horus banco_de_dados create.php on line 48 My line 48 : $fk_alunos = mysql_insert_id();&#xA; $queryInsert2 = $link->query("insert into pagamentos values('$fk_alunos', &#xA; default, '$situacao_aluno','$validade_plano',....

  • For better viewing: https://pastebin.com/wE5KcQQQJ

  • I would do differently $queryInsert1 = "Insert into student values(default,'$name' '$Cpf','$rg','$birth','$sex','$phone','$email','$address','$neighborhood', '$cep','$state','$city')"; $confirma_query1 = mysql_query($queryInsert1, $link); $id_do_Aluno_inserted above = mysql_insert_id(); $queryInsert2 = "Insert into payments values(". $id_do_Aluno_inserted above." , &#xA;$situacao_aluno','$validade_plano','$planos','$vencimento',&#xA;'$cpf_amigo','$forma_pagamento','$data_matricula',&#xA;'$numero_documento','$data_documento','$valor')"&#xA;&#xA;$confirma_query2 = mysql_query($queryInsert2, $link);

  • Brought this bug : Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\horus - Copia\banco_de_dados\create.php:49 Stack trace: #0 {main} thrown in C:\xampp\htdocs\horus - Copia\banco_de_dados\create.php on line 49 . Line 49 is $queryInsert1

Browser other questions tagged

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