How to pass SESSION to a variable in php

2

Hello. I would like to know how to pass the $ _SESSION code to a variable

if ( isset($_SESSION['codigoUsu']) ) {
    $codigoUsuario=$_SESSION['codigoUsu'];    
    inserirVenda($codigoUsuario,$codigoProduto);  
    header("Location: produtosSelecionados.php"); 
} 

Edited question

controlVend

<?php

include 'crudVenda.php';
session_start();
 $opcao=$_GET['opcao'];
 if($opcao=="selecionar"){
            $codigoProduto=$_GET['codigoProduto'];  
                if(isset($_SESSION['codigoUsu'])){
                    $codigoUsu=$_SESSION['codigoUsu'];
                    inserirVenda($codigoUsu,$codigoProduto);  

                }    

                header("Location: produtosSelecionados.php"); 




}?>

crudVenda

<?php
 include 'conexaoBD.php';
function inserirVenda($codigoUsuario,$codigoProduto){
    conectar();
    query("INSERT INTO venda (codigoProduto,codigoUsuario) VALUES ($codigoProduto,$codigoUsuario)");
    fechar();

}
?>
    
asked by anonymous 13.11.2018 / 01:57

2 answers

0

If your session does not have the UUsu, make sure your query is returning this user code when logging in (I assume it is an ID in the table). You can try something like this:

$sql = "SELECT * FROM <tblUsers> WHERE <condition>";

Assuming this table has the UUsu (or an ID, that is to say), use a fetch_array() (or similar) to transform the query result into an array, and store it in a variable called helper. After that, you just need to call the auxiliary variable and indicate the index (or key) of that array in it, for example:

$sql = "SELECT * FROM usuarios WHERE login = '$login' AND password = '$senha'";
$queryResult = mysqli_query($db_connect, $sql);
if(mysqli_num_rows($queryResult) == 1): //Se só 1 usuário for encontrado
$auxiliar = mysqli_fetch_array($queryResult);
echo $auxiliar['ID'];

To check the indexes that exist in your $auxiliar , check with var_dump(); .

    
13.11.2018 / 03:10
0

On every page that will use $ _SESSION you should start the function below.

session_start();
    
13.11.2018 / 02:10