I cannot recover id value through the POST method

Asked

Viewed 450 times

0

I have the following functions: File registra_account.php:

if(isset($_POST['acao'])){
  if($_POST['acao'] == "inserir"){
    inserirConta();
}
if($_POST['acao'] == "alterar"){
    alterarConta();


function selectIdConta($id){
    $banco = abrirBanco();
    $sql = "SELECT * FROM contas c INNER JOIN pessoa p ON(c.id_fornecedor = p.id) WHERE c.id = ".$id;
    $resultado = $banco->query($sql);
    $banco->close();
    $conta = mysqli_fetch_assoc($resultado);
    return $conta;
}



 function alterarConta(){
    $id_conta_selecionada = $_POST['id'];
    $sql = " UPDATE contas SET valor = '$valor' WHERE id= '$id_conta_selecionada' ";
    $banco->query($sql);
    $banco->close();

I realized that if I change the '$id_conta_selected' by an id registered in the database, the query works, however, through the POST method I am not able to get the account id.

File alterar_conta.php:

<?php
    include_once("registra_conta.php");
    require_once("conexao.php");

    if(!$_SESSION['usuario']){
        header('Location: index.php?erro=1');
    }

    $conta = selectIdConta($_POST["id"]);

?>

I have the form:

<form name="dadosConta" action="registra_conta.php" method="POST">

<input type="hidden" name="acao" value="alterar">
<input type="hidden" name="id" value="<?=$conta["id"]?>" />

        <div class="form-group">
            <button onclick="msgSucesso()" type="submit" value="Enviar" name="Enviar" class="btn customizado btn-roxo btn-lg">Alterar</button>
        </div>

</form>

'Cause I’m having trouble getting my account id back??inserir a descrição da imagem aqui

  • put by kindness the code that presets the value of this variable $account["id"]

  • $account = selectIdConta($_POST["id"]);

  • on the change side of your button puts this php code: .. <? php echo $account["id"]; ? > Change</button> if you do not show anything on the side of changing is because your method is wrong

  • I already know I’ll answer

  • Appeared "5", is the provider id, should get the account id. Probably there is something wrong in my query? Just can’t find where.

  • is because it spoke reference if * is which table, accounts or person. I’ll put in the answer

Show 1 more comment

1 answer

1


The problem is in your select query.

change that:

SELECT * FROM contas c INNER JOIN pessoa p ON(c.id_fornecedor = p.id) WHERE c.id = ".$id

for that reason:

SELECT c.* FROM contas c INNER JOIN pessoa p ON(c.id_fornecedor = p.id) WHERE c.id = ".$id

Because as you said in the comments is appearing the supplier id 5 and has no accounts with this id.

Browser other questions tagged

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