ID for undefined change

Asked

Viewed 43 times

0

Hello folks I’m having a problem trying to alter information on a criminal record. The system correctly fetches the ID to be changed and presents me on the screen with the inputs for the changes correctly, but when clicking the "Change" button it returns 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 undefined change"; which I reported on echo

  • Checks if the url is correct: ?id=4159 or &id=4159. Anything use the var_export($_GET);

1 answer

1

If you’re using POST on the form, you won’t be able to use the $_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"... use ID in form:

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

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

Browser other questions tagged

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