ID for change not defined

1

Hello everyone, I'm having a problem trying to change information in a cadastre. The system correctly looks up the ID to be changed and shows me on the screen with the inputs for the changes correctly, but clicking the "Change" button returns it as if there was no valid ID to change.

This is the validation code:

// pega o ID da URL
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;

// valida o ID
if (empty($id))
{
    echo "ID para alteração não definido";
    exit;
}

// busca os dados du usuário a ser editado
$PDO = db_connect();
$sql = "SELECT name, birthdate, gender, email, phone, mobile, endereco, numero, bairro, cidade, uf, cep, herois_idherois, entliga, cadvol FROM voluntarios WHERE idvoluntarios = :id";
$stmt = $PDO->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

$stmt->execute();

$user = $stmt->fetch(PDO::FETCH_ASSOC);

// se o método fetch() não retornar um array, significa que o ID não corresponde a um usuário válido
if (!is_array($user))
{
    echo "Nenhum usuário encontrado";
    exit;
}

I try to send POST already tried via GET to try to see what appears in the URL but it just closes and gives me the message "ID for change not defined"; which I reported in echo

    
asked by anonymous 12.01.2018 / 04:05

1 answer

1

If you are using POST in the form, you will not be able to use $_GET to get the ID, unless it is in the URL.

One solution is to do this:

if (isset($_GET['id'])) {
   $id = (int) $_GET['id'];
} elseif (isset($_POST['id'])) {
   $id = (int) $_POST['id'];
} else {
   echo "ID para alteração não definido";
   exit;
}

The other is instead of using <input name ="id"... using the ID in the form:

<form action="(endereco)?id=<?php echo $id;>" method="post">

So the parameters will be caught in $_GET even with method="post" .

    
12.01.2018 / 09:05