Pick variable on another page

Asked

Viewed 4,734 times

4

After fetching the data in mysql with php and displaying it on the page, I would like to know how to get these same results from this page where it is displayed and display them on another page.

<?php  

//Cria a variavel telefone enviada do formulario na pagina painel.php
$telefone = $_POST['telefone'];

//Verifica se o valor de telefone não é vazio.
if (empty($telefone)) { 
 header("location: painel.php");
 exit;
}
//Conecta com o banco e seleciona os arquivos para efetuar a contagem.
$con = mysqli_connect("localhost", "root", "", "pizzaria");
$query = mysqli_query($con, "SELECT nome, numero, endereco from clientes where telefone = $telefone");
$count = mysqli_num_rows($query);

//Se a contagem for 0
if ($count == 0) { ?>
<div class="alert alert-danger" role="alert">
    <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
    Ops! não encontramos nenhum cliente com este telefone,
    <a href="painel.php" class="alert-link">tente novamente!</a>
</div>
<?php
die;
//Se a contagem for 1
} else {
if ($count == 1) { ?>
    <div class="alert alert-success">
        Encontramos 1 cliente com este telefone!
    </div>
    <?php
}
}
//Mostra todos os resultados encontrados.
while ($resultado = mysqli_fetch_assoc($query)) {
echo "Cliente: ". $resultado['nome']. "<br/>";
echo "Endereço: ".$resultado['endereco']. "<br/>";
echo "N°: ".$resultado['numero']. "<br/>";
}

?>

I would like to take the results of the $result ['name'], $result['address'] and $result['number'] array and play them on the "new-request.php" page and then use them to insert into another table in the database.

  • using $_SESSION or sending by get or post..

  • Pay attention to SQL Injection.

  • will I have to create a "mini-form" to send? there is no other way?

  • http://php.net/manual/en/reserved.variables.session.php

  • only one remark: die() and exit() are the same things, so why not standardize using only one of them, besides, custom use return; instead of die() or exit(), because these methods were made to kill processes and not to implement output.

1 answer

3

You can use the session's from PHP. It is very easy to use and only needs to be started at the beginning of the script and then access through the global variable $_SESSION.

On the page that executes the query:

// no início do arquivo
session_start();

// após executar a query
$_SESSION["clientes"] = array();
while ($resultado = mysqli_fetch_assoc($query)) {
    $_SESSION["clientes"][] = $resultado;
}

On the other page:

// no inicio do arquivo
session_start();

while ($_SESSION["clientes"] as $cliente) {
    var_dump($cliente);
}

Important: the function session_start must be executed only once and before any command displaying information to the user, such as echo, print, ie, it is recommended to put right at the beginning of the file.

Obs: do not recommend passing the results of a full search via $_SESSION. An alternative would be to save the filters or even the query in Session and then run again on the other page.

Browser other questions tagged

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