-2

thank you for reading my message. I am working on a website, but whenever I want to display a number from a database field on the website, no errors, but nothing is shown.

(sorry for bad english)

auth

session_start();
if(!isset($_SESSION["username"])) {
    header("Location: login.php");
    exit();
}

login

  require('db.php');
session_start();
// When form submitted, check and create user session.
if (isset($_POST['username'])) {
    $username = stripslashes($_REQUEST['username']);    // removes backslashes
    $username = mysqli_real_escape_string($con, $username);
    $password = stripslashes($_REQUEST['password']);
    $password = mysqli_real_escape_string($con, $password);
    // Check user is exist in the database
    $query    = "SELECT * FROM `users` WHERE username='$username'
                 AND password='" . md5($password) . "'";
    $result = mysqli_query($con, $query) or die(mysql_error());
    $rows = mysqli_num_rows($result);
    if ($rows == 1) {
        $_SESSION['username'] = $username;
        $_SESSION['coins'] = $coins;
        // Redirect to user dashboard page
        header("Location: dashboard-2.php");
    } else {
        echo "<div class='form'>
              <h3>Je wachtwoord of gebruikersnaam is onjuist.</h3><br/>
              <p class='link'>Klik hier om <a href='login.php'>terug</a> te gaan.</p>
              </div>";
    }
} else {

coins need to be shown on the dashboard.

w 2
  • 1
  • 1

2 Answers2

1

There are several things wrong here:

  1. mysql_error() cannot be used with "mysqlixxx": they're two completely different libraries.
    Use mysqli::error instead.

  2. You should NEVER, EVER, NEVER EVER use raw user input in a SQL statement. Use prepared statements instead.

  3. Finally, the reason you're not seeing "coins" ... is because you never read anything from the result set.

    SUGGESTION: $coins = $rows[0]['coins'];

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
0

You're setting $_SESSION['coins'] to an undefined variable ($coins). You have to fetch a row from the result set, e.g. mysqli_fetch_object

Try this:

    if ($rows == 1) {
        $row = mysqli_fetch_object($result);
        $_SESSION['username'] = $username;
        $_SESSION['coins'] = $row->coins; //Your column for coins in your table.
        // Redirect to user dashboard page
        header("Location: dashboard-2.php");
    } else {
        echo "<div class='form'>
              <h3>Je wachtwoord of gebruikersnaam is onjuist.</h3><br/>
              <p class='link'>Klik hier om <a href='login.php'>terug</a> te gaan.</p>
              </div>";
    }
mikaelwallgren
  • 310
  • 2
  • 9