2
I need to get the user ID already logged in to play in an Insert query, because I will connect it to a Foreign Key Employee. I need to get this ID, only I’m not sure how to proceed and I’m beginner in php.
Login script:
<?php
SESSION_start();
include('conexao.php');
if (empty($_POST['usuario']) || empty($_POST['senha'])) {
header("location: index.php");
exit();
}
$usuario = mysqli_real_escape_string(conexao(), $_POST['usuario']);
$senha = mysqli_real_escape_string(conexao(), ($_POST['senha']));
$query = "select * from funcionarios where login='{$usuario}' and senha = '{$senha}'";
$result = mysqli_query(conexao(), $query);
$row = mysqli_num_rows($result);
$error = "Usuário ou Senha incorretos";
if ($row > 0) {
$_SESSION['usuario'] = $usuario;
$_SESSION['senha'] = $senha;
header('Location: paginaConsulta.php');
exit();
} else {
unset ($_SESSION['usuario']);
unset ($_SESSION['senha']);
$_SESSION["invalido"] = $error;
header("location: index.php");
}
Part of the Insert:
//Caso não ocorra nenhum erro, permita que os dados sejam inseridos no banco.
if ($row == 0) {
$link = conexao();
$query = "insert into clientes(nome_cliente, cnpj)
values('{$nome_cliente}', '{$cnpj}')";
$result = mysqli_query($link, $query);
//Recupero o id do cliente que acabou de ser inserido
$idCliente = mysqli_insert_id($link);
//Recupero o id do funcionaio que acabou de ser inserido
$idFuncionario = $_SESSION["usuario"];
$query2 = "insert into erros(tipo_erro,solucao,data_ocorrencia,sistema, Fk_Funcionarios, Fk_Clientes)
values('{$nome_erro}', '{$solucao}', '{$data}', '{$sistema}', '{$idFuncionario}', '{$idCliente}')";
$result2 = mysqli_query($link, $query2);
}
header('location: paginaConsulta.php');
}
How do I declare a new session to pull up the ID? I am sending the form via post, and the ID is in AUTO INCREMENT in the database.
I don’t know how to recover the logged-in user ID in the session so I can play in this $Idfuncionaio variable.
The part of idClients is working.
Possible duplicate of Grab user id by SESSION
– Patrick Perdigão
What the function
conexao
does? If it returns a connection to the bank there is much that can improve the code.– Woss
I managed to solve, with the solution of the companions below. What I could improve in the code?
– Vithor Carlos