-1
I have a table containing user id, email, password and rating (admin or not). When a person logs in, I’m making them click to other sessions as the system introduces expenses into a database and has to be associated with each person. When I try to load the user id, I’m having problems, saying that the second "Row" is undefined. How does this happen if my first Row worked? How can I fix it? I also left the second expense area code, where I’m having trouble picking up the user id and playing along with the expense.
Edit: I forgot to point out that many cases here in the stack relate to the existing id function (for each connection). In this case I put here, is the id I put myself for each user in the database.
if(isset($_POST['submit'])):
$erros = array();
$username = mysqli_escape_string($connect, $_POST['email']);
$password = mysqli_escape_string($connect, $_POST['senha']);
if(empty($username) or empty($password)):
$erros[] = "<li>Todos os campos devem ser preenchidos.</li>";
else:
$sql = "SELECT username FROM user WHERE username = '$username'";
$resultado = mysqli_query($connect, $sql);
if(mysqli_num_rows($resultado) > 0):
$password = md5($password);
$sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password'";
$resultado = mysqli_query($connect, $sql);
if(mysqli_num_rows($resultado) == 1):
$dados = mysqli_fetch_assoc($resultado);
if ($dados['admin'] == '1'):
mysqli_close($connect);
$id_user = $_REQUEST['id_user'];
$_SESSION['login'] = $username;
$_SESSION['senha'] = $password;
$_SESSION['id_user'] =$row['id_user'];
header('Location: areaAdm.php');
else:
mysqli_close($connect);
$_SESSION['login'] = $username;
$_SESSION['senha'] = $password;
$_SESSION['id_user'] = $row['id_user'];
header('Location: areaUsuario.php');
endif;
<?php
session_start();
include('classes/db_connect.php');
if(isset($_POST['submit']))
{
//definindo todas as variáveis para preencher a tabela
//sessao do usuario
$id_user = $_SESSION['id_user'];
//definindo formato do arquivo
$extensao = strtolower(substr($_FILES['fotoDespesa']['name'], -4));
$novo_nome = md5(time()). $extensao;
$diretorio = "uploads/";
move_uploaded_file($_FILES['fotoDespesa']['tmp_name'], $diretorio.$novo_nome);
//destino da despesa
$tipoReembolso= $_POST['tipoReembolso'];
//tipo da despesa
$type=$_POST['tipoDespesa'];
//data formatada para dia-mes-ano)
$olddate=$_POST['data'];
$date = date('d-m-Y', strtotime($olddate));
//quilometragem (se for o caso)
$km =$_POST['numeroKm'];
//preço despesa
$price=$_POST['precoDespesa'];
//mensagem de observação
$message=$_POST['observacao'];
$insert="INSERT INTO despesas(
id_user,
tipoDespesa,
dataDespesa,
precoDespesa,
fotoDespesa,
tipoReembolso,
observacao)
VALUES(
'$id_user',
'$type',
'$date',
'$price',
'$novo_nome',
'$tipoReembolso',
'$message')";
if(mysqli_query($connect,$insert)){
echo "Sucesso ao enviar a despesa!";
}
else {
echo "Erro ao enviar! Por favor, preencha todos os campos.";
}
}
?>
The error in the second case, related to the id_user session and I believe it is linked to the login page.
Correct would not be $data['id_user']; ? see $data = mysqli_fetch_assoc($result);
– user60252