0

Here is my home.php where you will see the fields of the two tables, students and payments:

<?php

include_once 'config.php';
global $pdo;

//Here you can see the fields in the student table: nome, fone and email. 
//And only one field in the pay table that is: situacao_aluno

$sql = "select * from alunos left join pagamentos on id_alunos = 
pagamentos.alunos_id order by nome";
$sql = $pdo->query($sql);

if ($sql->rowCount() > 0) {
   foreach ($sql->fetchAll() as $aluno):
?>

    <tr>
     <td> <?php echo $aluno['nome']; ?> </td>
     <td> <?php echo $aluno['fone']; ?> </td>
     <td> <?php echo $aluno['email']; ?> </td>
     <td> <?php echo $aluno['situacao_aluno']; ?> </td>

<?php
   echo "<td><a href='editar.php?id_alunos=" . $aluno['id_alunos'] . "' 
   class='btn btn-dark' role='button'>Update</a></td>";

   echo "<td><a href='delete_submit.php?id_alunos=" . $aluno['id_alunos'] . 
   "' class='btn btn-danger'>Delete</a></td>";
   echo "</tr>";

?>

Okay, cool, let's get edit as an example: send to editar.php page, on this page, I have more fields for these editors. A part of php on this page looks like this:

<?php
include_once 'includes/header.inc.php';
include_once 'includes/menu.inc.php';
include_once 'config.php';
?>

<?php
 
$id_alunos = (isset($_GET['id_alunos'])) ? $_GET['id_alunos'] : '';

if (!empty($id_alunos) && filter_var($id_alunos, FILTER_VALIDATE_INT)):

$ret = array();

global $pdo;
$sql = "select * from alunos left join pagamentos on alunos.id_alunos = 
pagamentos.alunos_id where alunos.id_alunos = $id_alunos";

$sql = $pdo->query($sql);
if ($sql->rowCount() > 0):
    $ret = $sql->fetchAll()[0];
endif;

if(empty($ret)) {
header('Location: home.php');
    
    exit(0);
 }
endif;

?>

Soon after this code comes the fields that are edited (like a form). i have the form action=" " ,I send to the editar_submit.php page that has this code:

<?php
require 'config.php';
require 'class/crud.php';

$id_alunos = (isset($_POST['id_alunos'])) ? $_POST['id_alunos'] : 0;
if(empty($id_alunos)){
header('Location: home.php');
exit(0);
}

$altera = new Alunos();

$nome = addslashes($_POST['nome']);
$rg = addslashes($_POST['rg']);
$cpf = addslashes($_POST['cpf']);
$nascimento = addslashes($_POST['nascimento']);
$sexo = addslashes($_POST['sexo']);
$fone = addslashes($_POST['fone']);
$email = addslashes($_POST['email']);
$endereco = addslashes($_POST['endereco']);
$bairro = addslashes($_POST['bairro']);
$cidade = addslashes($_POST['cidade']);
$estado = addslashes($_POST['estado']);
$cep = addslashes($_POST['cep']);
$situacao_aluno = addslashes($_POST['situacao_aluno']);
$vencimento_plano = addslashes($_POST['vencimento_plano']);
$planos = addslashes($_POST['planos']);
$vencimento = addslashes($_POST['vencimento']);
$cpf_amigo = addslashes($_POST['cpf_amigo']);
$forma_pagamento = addslashes($_POST['forma_pagamento']);
$data_matricula = addslashes($_POST['data_matricula']);
$numero_documento = addslashes($_POST['numero_documento']);
$data_documento = addslashes($_POST['data_documento']);
$valor = addslashes($_POST['valor']);

$altera->UpdateAlunos($id_alunos, $nome, $rg ,$cpf, $nascimento, $sexo, 
$fone, $email, $endereco, $bairro, $cidade, $estado, $cep);

$altera->UpdatePagamentos($id_alunos, $email, $situacao_aluno, 
$vencimento_plano, $planos,$vencimento, $cpf_amigo, $forma_pagamento, 
$data_matricula, $numero_documento, $data_documento, $valor);

header('Location: editar.php?id_alunos='.$id_alunos);
?>

Here is my class/crud.php has a class Students that have all querys:

 //Here in the case I will only show the UPDATE, 

/*
* class UpdateAlunos()
* edit the table students
*/

public function UpdateAlunos($id_alunos, $nome, $rg, $cpf, $nascimento, 
$sexo, $fone, $email, $endereco, $bairro, $cidade, $estado, $cep)
{

    global $pdo;


$sql = $pdo->prepare("update alunos set nome=:nome, rg=:rg, cpf=:cpf, 
nascimento=:nascimento, sexo=:sexo, fone=:fone, email=:email, 
endereco=:endereco, bairro=:bairro, cidade=:cidade, estado=:estado, cep=:cep 
where id_alunos = '$id_alunos'");

    $sql->bindValue(":nome", $nome);
    $sql->bindValue(":rg", $rg);
    $sql->bindValue(":cpf", $cpf);
    $sql->bindValue(":nascimento", $nascimento);
    $sql->bindValue(":sexo", $sexo);
    $sql->bindValue(":fone", $fone);
    $sql->bindValue(":email", $email);
    $sql->bindValue(":endereco", $endereco);
    $sql->bindValue(":bairro", $bairro);
    $sql->bindValue(":cidade", $cidade);
    $sql->bindValue(":estado", $estado);
    $sql->bindValue(":cep", $cep);
    $sql->execute();

}

/*
* class UpdatePagamentos()
* edit the payments table
*/

public function UpdatePagamentos($situacao_aluno, $vencimento_plano, 
$planos, $vencimento, $cpf_amigo, $forma_pagamento, $data_matricula, 
$numero_documento, $data_documento, $valor)
{

    global $pdo;


$sql = $pdo->prepare("update pagamentos set situacao_aluno=:situacao_aluno,
vencimento_plano=:vencimento_plano, planos=:planos, vencimento=:vencimento, 
cpf_amigo=:cpf_amigo, forma_pagamento=:forma_pagamento, 
data_matricula=:data_matricula, numero_documento=:numero_documento,
data_documento=:data_documento, valor=:valor where alunos_id = 
'$id_alunos'");
    $sql->bindValue(":situacao_aluno", $situacao_aluno);
    $sql->bindValue(":vencimento_plano", $vencimento_plano);
    $sql->bindValue(":planos", $planos);
    $sql->bindValue(":vencimento", $vencimento);
    $sql->bindValue(":cpf_amigo", $cpf_amigo);
    $sql->bindValue(":forma_pagamento", $forma_pagamento);
    $sql->bindValue(":data_matricula", $data_matricula);
    $sql->bindValue(":numero_documento", $numero_documento);
    $sql->bindValue(":data_documento", $data_documento);
    $sql->bindValue(":valor", $valor);
    $sql->execute();

}

OK! I managed to make the edit work just on the table students !!! The situacao_aluno field of the payments table does not appear even in my home.php, and neither does the edit work in the payments table! What do you think I got wrong?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • Not related but it would help to take a look [PHP - why global variables are evil](https://stackoverflow.com/questions/48124793/php-why-global-variables-are-evil) – Masivuye Cokile Oct 23 '18 at 14:57
  • To update two tables at once, you need to use MySQL's multi-table update syntax (https://dev.mysql.com/doc/refman/8.0/en/update.html). Also note that it helps to avoid confusion (such as here, where we don't see the table structures) if you specify both table (alias) and column name (e.g. `alunos.nome`) in the query when using JOINs. – Dormilich Oct 23 '18 at 16:07

0 Answers0