首页 > 解决方案 > 用两个表更新 - php

问题描述

这是我的home.php,您将在其中看到两个表的字段,学生和付款:

<?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>";

?>

好的,很酷,让我们以编辑为例:发送到editar.php 页面,在这个页面上,我为这些编辑器提供了更多字段。此页面上的部分php如下所示:

<?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;

?>

在这段代码之后不久,就会出现被编辑的字段(如表单)。我有表单 action=" ",我发送到具有以下代码的editar_submit.php 页面:

<?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);
?>

这是我的class/crud.php有一个有所有查询的学生类:

 //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();

}

好的!我设法在学生桌上进行编辑!付款表的situacao_aluno字段甚至没有出现在我的home.php中,付款表中的编辑也不起作用!你觉得我做错了什么?

标签: phppdoinsert-update

解决方案


推荐阅读