How can I create multiple $_SESSION in one function?

Asked

Viewed 46 times

0

To be clear, I am developing a simple PHP and SQL application, which has been breaking my head in the last few days...

<?php
session_start();

    include 'header.php';

    include 'db.php';

    $pesquisa = isset($_POST['busca']) ? $_POST['busca'] : '';
    $consulta = "SELECT * FROM clientes WHERE nome LIKE '%$pesquisa%'";
    $con = $mysqli->query($consulta) or die($mysqli->error);
?>

    <body>
        <div class="container">
            <div class="title" style="margin-top: 5px;">
                <h3>CLIENTES</h3></div>
            <div class="row" style="margin-top: 5px;">
                <?php while ($dados = $con->fetch_array()) { ?>
                    <div class="col">
                        <div class="card" style="width: 18rem;">
                            <img src="clientes/<?php echo $dados["foto"] ?>" class="card-img-top" alt="client" style="width: 350px; height: 300px;">
                            <div class="card-body">
                                <h5 class="card-title"><?php echo $dados['nome'] ?></h5>
                                <p class="card-text"><?php echo $dados['telefone'] ?></p>
                                <p class="card-text">Cliente nº<?php echo $dados['id']; 
                                $_SESSION["ID"] = $dados['id']; ?></p>
                                <a href="teste.php" class="btn btn-primary">Ler Mais</a>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
    </body>

this is the page where it displays the content in my SQL database. Everything goes well, until the moment I need to open on another page only the content referring to a unique ID, as the SESSION I created there. However, although inside a repeating structure, that vaiavel $_SESSION only recognizes the first ID.

<?php
session_start();
include 'db.php';
	$id = $_SESSION["ID"];
	$consulta = "SELECT * FROM clientes WHERE id = '$id'";
    $con = $mysqli->query($consulta) or die($mysqli->error);

    while($dados = $con->fetch_array()){
    	echo $dados = $dados['id'];

  		
    }


?>

And whenever I try to look for this super global here on this test page, it returns me the same result, which is always the first data entered in the bank, no matter how many I put. Someone please give me a light to get out of this impasse?

  • have a few things here to comment on: 1) Yes inside a while: Session will save only the last (each while iteration will override the previous value) value of the while, then this is meaningless, unless you saved an array of values. 2) made a select with like which can return more than one record, so the way you used Session makes no sense, it should be an array. 3)on the second page, if it is a query per unique id as you said, then it will only return a value, q may be what you meant by "returns the same result", then the behavior is right.

  • echo $dados = $dados['id']; this will return only id with only 1 element "which is always the first data inserted in the database"

No answers

Browser other questions tagged

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