GET PHP MYSQL UPDATE

Asked

Viewed 73 times

-3

Hey, guys, I have a question. I have a page from udpate using the $_GET to get the url, but in the update form I played the action in another folder, then at the time of doing the update does not recognize the ID, as it left the current page, as I can solve this, since it gave problem putting the code on the same page of the form, because only updated by clicking the button without filling. and also for code organization, follow update code

$ID        = isset($_GET['ID']) ? $_GET['ID'] : '';
$titulo    = isset($_POST['titulo']) ? $_POST['titulo'] : '';
$descricao = isset($_POST['descricao']) ? $_POST['descricao'] : '';
$alt       = isset($_POST['alt']) ? $_POST['alt'] : '';

$bd->query("UPDATE tortas SET titulo = '$titulo', descricao = '$descricao', alt = '$alt' WHERE ID = '$ID'");
  • You can pass the ID on an input of type Hidden, so you will be able to catch it on the other page using the global variable $_POST['id']

  • In the PHP file that has the form, you have to send the data to the new PHP where you have the SQL statement, I make me understand?

  • Leaves on the same page of the form and puts the UPDATE inside a if checking that the variables are full.

1 answer

1


You can do it this way:

// Página 1 - Onde tem o formulário
// Vamos supor que a URL é: http://localhost/noticia/editar?id=15

<input type="hidden" name="id" value="<?php echo $_GET['id'] ?>" />
<input type="text" name="titulo" />
<input type="text" name="descricao" />
// Página 2 - Onde vai atualizar

$ID        = $_POST['ID'];
$titulo    = isset($_POST['titulo']) ? $_POST['titulo'] : '';
$descricao = isset($_POST['descricao']) ? $_POST['descricao'] : '';
$alt       = isset($_POST['alt']) ? $_POST['alt'] : '';

// O ideal seria você validar se o ID foi informado
if ($ID !== null) {
    // Tratar erro...
}

$bd->query("UPDATE tortas SET titulo = '$titulo', descricao = '$descricao', alt = '$alt' WHERE ID = '$ID'");

Browser other questions tagged

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