Show column sum of the logged in user. error

0

Good morning, someone help me? I do not understand much of php, but for me to finish my site there is only one thing: I need to post the sum of the values that are in the table but based on the logged in user, already tried in all the ways but I can not follow the last one that I tested :

<?php    
    $_SESSION['usuario'] = 'user_login';
    $sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario= usuario");

    //Passa o ID do usuário para a query
    $sql->bindValue(1, $_SESSION['user_login'], PDO::PARAM_INT);
    $sql->execute();
    $ln = $sql->fetchObject();
    echo $ln->total;    
?>

I have tried everything or even just get values from the column without being a user logged in. The page is completely blank.

    
asked by anonymous 20.04.2018 / 16:50

4 answers

0

I think the problem is in your query. At the end "WHERE user = user", the user is as a string. You need to leave it as a variable, like this: '$ user'.

I've never used PHP that way, object-oriented, but I think that should resolve.

    
20.04.2018 / 17:02
0

Do this:

$usuario = $_SESSION['usuario'];

$sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario= $usuario");
    
20.04.2018 / 18:28
0

I believe that your problem is in passing the value to be replaced in the query.

$sql->bindValue(1, $_SESSION['user_login'], PDO::PARAM_INT);

The above line will replace the first character "?" of your query by the second parameter passed in the call of the bindValue method that in your case is $ _SESSION ['user_login'], however your query does not have a "?" character. So you should replace your query with the following:

$sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario = ?");
    
20.04.2018 / 18:35
0

Try replacing the PDO parameter like this:

<?php    
    $_SESSION['usuario'] = 'user_login';
    $sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario= :usuario");

    //Passa o ID do usuário para a query
    $sql->bindValue(':usuario', $_SESSION['user_login'], PDO::PARAM_INT);
    $sql->execute();
    $ln = $sql->fetchObject();
    echo $ln->total;    
?>

Make sure that the session variable is coming correct, add some var_dump or print_r to see what its value is, and go ahead.

    
24.04.2018 / 20:13