How to make a global variable in PHP?

Asked

Viewed 638 times

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 the information is stored in a comic book. 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 id_patient to the file "patiente_marcar_consult_online.php" where I would then present the name of this id_patient. Can you help me?

  • Related https://answall.com/q/49521/101, https://answall.com/q/102652/101

1 answer

0


You can use $_SESSION to store the variable in Cookies!

At the beginning of the file, right at the top type this code:

<?php session_start(); ?>

This will allow you to store 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 to send, but change the name if you wish.

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

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

More Information

Once you have the patient id, do a query to fetch the patient data.

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

Now all that remains is to print the name

<label><?=$row['nome']; ?></label>
  • but how do I display on a label the associated name in the database to that id_patient

  • You will display the data with echo $_SESSION['name']; but at the beginning when you log in, save its name also in a session variable .

  • @Isabelsousa Look what I added

  • @Brunomoutinho I have a combobox that is filled in by the database and when I select a name, fill in the respective form. I store the id like this: echo '<option value="'. $Prod['txtid']. '">'. $Prod['txtNome']. '</option>';' but needed to use this id for query delete, the only problem is that this query is in a PHP file that is called to the main file where the combobox is.

Browser other questions tagged

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