Capture session data with PHP and Mysql

Asked

Viewed 520 times

-1

I am trying to make a system in which in my user database has 4 fields: usuario_id, usuario_nome, usuario_senha and usuario_empresa_id.

I need to capture the data of usuario_empresa_id (who receives the id table empresa when a user makes a company registration together with his login) and put inside the field empresa_id table deposito, but inside, when I try to recover the usuario_empresa_id who is in the Session, he always returns 0. Someone could explain to me why?

Follows the code:

<?php
session_start();

include('conexao.php');

if(empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.php');
    exit();
}

$usuario = mysqli_real_escape_string($conn, $_POST['usuario']);
$senha = mysqli_real_escape_string($conn, $_POST['senha']);

$query = ("SELECT usuario_id, usuario_nome FROM usuarios 
WHERE usuario_nome = '$usuario' AND usuario_senha = '$senha'");

$result = mysqli_query($conn, $query);

$row = mysqli_num_rows($result);

$QR = ("SELECT usuario_empresa_id FROM usuarios WHERE usuario_nome = '$usuario' AND usuario_senha = '$senha'");
$result_qr = mysqli_query($conn, $QR);
$idempresa = mysqli_num_rows($result_qr);

if($row == 1) {
    $_SESSION['usuario'] = $usuario;
    header('Location: painel.php');
    exit();
} else {
    $_SESSION['nao_autenticado'] = true;
    header('Location: index.php');
    exit();
}

if ($_SESSION['usuario']){
    $_SESSION['idempresa'] = $idempresa;
}
?>

follows the insertion code in the deposit table

<?php
session_start();
include("conexao.php");


$empresa = $_SESSION['idempresa'];


$deposito = mysqli_real_escape_string($conn, $_POST['deposito']);

$result_deposito = "INSERT INTO depositos(
'deposito_nome', 
'deposito_empresa_id') 
 VALUES( 
'$deposito',
'$empresa'
)";
$resultado_deposito = mysqli_query($conn, $result_deposito);

?>
  • Already gave a var_dump in the variable $idempresa?

  • Yeah, it doesn’t point to anything

  • I think you could explain better. In the code shows nothing related to the table "deposit" cited, and in no time you are using usuario_empresa_id of the query’s SELECT $QR. The only thing I see you do is count Rows with mysqli_num_rows.

  • the insertion in the deposit table I try to do in another file calling the $_SESSION['idempresa']. you could tell me how I use the query then?

  • I didn’t quite understand your question. You want to take the amount of $idempresa and put within a session to call elsewhere?

2 answers

0

You tried to:

// user name.

$idempresa = $result_qr['usuario_empresa_id'];
$_SESSION['idempresa'] = $idempresa;

Then just troccar.

if($idempresa == 1) {
    $_SESSION['usuario'] = $usuario;
    header('Location: painel.php');
    exit();
} else {
    $_SESSION['nao_autenticado'] = true;
    header('Location: index.php');
    exit();
}

-1


You close the script before adding $idempresa in the session.

Take this code

$_SESSION['idempresa'] = $idempresa;

and put inside the

if($row == 1) {
    $_SESSION['idempresa'] = $idempresa;

Edit:

On the line:

$idempresa = mysqli_num_rows($result_qr);

Voce is picking up the amount of lines mysqli_num_rows and not the value of the record. Replace with mysqli_query

Edit 2:

To see the data obtained from the user use var_dump in the variable $db_user

$result_qr = mysqli_query($conn, $QR);
$db_user = $result_qr->fetch_assoc();

if($row == 1) {
    $_SESSION['idempresa'] = $db_user['usuario_empresa_id'];
    $_SESSION['usuario'] = $usuario;
    header('Location: painel.php');
    exit();
} else {
    $_SESSION['nao_autenticado'] = true;
    header('Location: index.php');
    exit();
}
?>

Edit 3:

<?php
session_start();

include('conexao.php');

if(empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.php');
    exit();
}

$usuario = mysqli_real_escape_string($conn, $_POST['usuario']);
$senha = mysqli_real_escape_string($conn, $_POST['senha']);

$query = ("SELECT usuario_id, usuario_nome, usuario_empresa_id FROM usuarios WHERE usuario_nome = '$usuario' AND usuario_senha = '$senha'");

$result = mysqli_query($conn, $query);
$row = mysqli_num_rows($result);
$usuario = $result->fetch_assoc();

if($row == 1) {
    $_SESSION['idempresa'] = $usuario['usuario_empresa_id'];
    $_SESSION['usuario'] = $usuario['usuario_nome'];
    header('Location: painel.php');
    exit();
} else {
    $_SESSION['nao_autenticado'] = true;
    header('Location: index.php');
    exit();
}

?>
  • I removed Exit(); from if and now giving an echo in the variable $company = $_SESSION['idempresa']; it gives the result as 1, even if the user is not 1. You know how to solve?

  • What value of the $Row variable?

  • is 1, but if I change for > 0 for example it does not login my system, I do not know the reason... Would you know some way to get the user?

  • I made a change in response, you tested that way?

  • yes, keep returning 1

  • I edited the answer again, it seems that the error is in the variable idempresa

  • you know which one I should use then? the mysqli_fetch_array and mysqli_fetch_assoc return this error: Notice: Array to string Conversion

  • Put the variable var_dump $result_qr, there must be the value you need, or you can assign direct in the variable $idempresa = $result_qr['idempresa'], or something like.

  • I don’t know how to display the value of this var_dump, because this code is in the login.php file that once the login and password is verified redirects to the.php panel, it doesn’t stay there to see the var_dump, and if I go straight to this login.php it won’t be in a Session. the other Rma did not work, from a blank page

  • put before the if($row == 1) { the var_dump($result_qr);exit();

  • the result is: Object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }

  • worked out, buddy. thanks

Show 7 more comments

Browser other questions tagged

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