How to make a global variable in PHP?

0

I have a login, and if the credentials are correct it opens another PHP file and wanted the name of the person associated with the credentials used in the login to appear on a label. All information is stored in a database. How do I access the values stored in the first PHP file?

The login code is:

$email = mysqli_real_escape_string($dbconn, $_POST['login']);
    $password = mysqli_real_escape_string($dbconn, $_POST['senha']);

    if (!empty($email) && !empty($password))
    {

        $query = "SELECT email, password, id_paciente FROM Paciente WHERE email = '$email' AND password = '$password'";
        $data = mysqli_query($dbconn, $query);
        $result = mysqli_num_rows($data);

    if ($result == 1) 
    {
                $row = mysqli_fetch_array($data);
                 $_SESSION['id_paciente'] = $row['id_paciente'];
                 $_SESSION['nome'] = $row['nome'];
                header("location: paciente_marcar_consulta_online.php");
        } 

And I wanted to pass the patient_id to the file "patient_marke_consult_online.php" in which the name of that patient_id was then presented. Can you help me?

    
asked by anonymous 01.07.2017 / 11:55

1 answer

0

You can use $ _SESSION to save the cookie in your cookie!

At the top of the file, even at the top, enter this code:

<?php session_start(); ?>

This will allow you to save variables in $_SESSION . Then add this to your code:

if (!empty($email) && !empty($password))
    {

        $query = "SELECT email, password, id_paciente FROM Paciente WHERE email = '$email' AND password = '$password'";
        $data = mysqli_query($dbconn, $query);
        $result = mysqli_num_rows($data);

    if ($result == 1) 
    {
                $row = mysqli_fetch_array($data);
                $_SESSION['id_paciente'] = $row['nome_da_variavel'];
                header("location: paciente_marcar_consulta_online.php");
        } 

As you can see, I just changed the variable you wanted, but change the name if you wish.

Now on page paciente_marcar_consulta_online.php , at the top of the page type the same code <?php session_start(); ?> and then define a variable and go fetch the value sent by SESSION, ie

<?php $id_paciente = $_SESSION['id_paciente']; ?>

More Information

Once you have the patient id, query the patient data.

$query = "SELECT FROM Paciente WHERE id_paciente= '$id_paciente'";
$data = mysqli_query($dbconn, $query);
$row = mysqli_fetch_array($data);

Now all you need to do is print the name

<label><?=$row['nome']; ?></label>
    
01.07.2017 / 12:10