How to pass SESSION to a php variable

Asked

Viewed 265 times

2

Hi. I was wondering how can I pass the $_SESSION code to a variable

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

Edited question

controleVenda

<?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();

}
?>
  • What exactly doesn’t work on your code snippet? Are you going to use this variable elsewhere? You are starting the section on the page products.php? Ever tried to give a var_dump() in its variable?

  • I take this $code and insert it into the bank and it goes to this page where you selected this product. But it is giving an error where it says q n recognizes the codeUsu. I am starting the session on another control page.

  • I have a page where are the products to select. Clicking there it goes to the controlVenda which is the page where I get what is sent from the page that has the products selectable. In this page of control I need to take the user code via session to send to function and insert in the bank

  • It is needed by simple quotes on the variables present in the VALUES of the sql command.

2 answers

0

On all pages that will use $_SESSION you should start the function below.

session_start();
  • It is usual and always good if the session is not being initialized by a library if ( !isset($_SESSION) ) {} to check the global variable because one page may be loading others that already contain a call to instantiate the section.

  • The problem is that it is not entering in the bank

0


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

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

Assuming that this table has the codeUsu (or an ID, better said), use a fetch_array() (or similar) to turn the query result into an array, and store it in a so-called auxiliary variable. 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 Dexes that exist in your $auxiliar, check with var_dump();.

Browser other questions tagged

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