How to get value from a table column [closed]

0

I wanted to get the points value of the logged in user and put it on my page. This is the table "users"

Forexample:"You have x points"

Below page login.php

  <?php 
  $login = $_POST['login'];
  $entrar = $_POST['entrar'];
  $senha = md5($_POST['senha']);
  $connect = mysql_connect('xxxx','xxxxx','xxxxxxx');
  $db = mysql_select_db('xxxxxxxxx');
    if (isset($entrar)) {

      $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar");
        if (mysql_num_rows($verifica)<=0){
          echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
          die();
        }else{
          setcookie("login",$login);
          header("Location:index.php");
        }
    }
?>
    
asked by anonymous 08.12.2018 / 23:44

2 answers

0

I have edited the response and updated to MySQLi because the version you are using MySQL is deprecated

$connect = new mysqli ("localhost", "USUARIO", "SENHA", "Nome_DB");

$sql="SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'";
$buscar=mysqli_query($connect,$sql);
$dados=mysqli_fetch_array($buscar);

$result=mysqli_num_rows($buscar);

if ($result==1) {

    $pontos = $dados["pontos"];

    setcookie("login",$login);
    header("Location:index.php?pontos=".$pontos);

}else|{

    echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
    die();

}

In the index page retrieve via GET

$pontos = $_GET['pontos'];

Now you can put it where you want.

  

within PHP

 echo $pontos;
  

within HTML

<?php echo $pontos ?>
    
09.12.2018 / 01:35
0

There is already a thread explaining how to do this, not specifically your problem, but as you said you are a layperson in the language I will explain how to answer.

This is your code (apparently it's working)

  <?php 
  $login = $_POST['login'];
  $entrar = $_POST['entrar'];
  $senha = md5($_POST['senha']);
  $connect = mysql_connect('xxxx','xxxxx','xxxxxxx');
  $db = mysql_select_db('xxxxxxxxx');
    if (isset($entrar)) {

      $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar");
        if (mysql_num_rows($verifica)<=0){
          echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
          die();
        }else{
          setcookie("login",$login);
          header("Location:index.php");
        }
    }
?>

In this line $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar"); you are selecting everything from the usuarios table where the credentials are in agreement.

This query is bringing all the data in the table as long as the data you entered is ok.

In this line if (mysql_num_rows($verifica)<=0) you are checking if the user does not exist, in the else you are saving the login in a session, the logic is the same, just block else add $fetch = mysql_fetch_object($verifica); and access the value of points $fetch->pontos;

In the end your code would be

  <?php 
  $login = $_POST['login'];
  $entrar = $_POST['entrar'];
  $senha = md5($_POST['senha']);
  $connect = mysql_connect('xxxx','xxxxx','xxxxxxx');
  $db = mysql_select_db('xxxxxxxxx');
    if (isset($entrar)) {

      $verifica = mysql_query("SELECT * FROM usuarios WHERE login = '$login' AND senha = '$senha'") or die("erro ao selecionar");
        if (mysql_num_rows($verifica)<=0){
          echo"<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='login.html';</script>";
          die();
        }else{
          setcookie("login",$login);

          $fetch = mysql_fetch_object($verifica);
          $_SESSION["pontos"] = $fetch->pontos;
          header("Location:index.php");
        }
    }
?>

To access the session value in index.php simply log in to the file and get the value.

index.php

session_start ();

echo "you have". $ _SESSION ["points"]. "points";

    
09.12.2018 / 01:38