0

I'm working on a website with where a user profile page is part of.

This is my code on top of the page:

<?php

require 'includes/connect.inc.php';

    session_start();

$id = $_SESSION['user_id'];
$userResult = mysqli_query("SELECT * FROM users where user_id ='$id'");
while($userRow = mysqli_fetch_array($userResult)) {
    $avatar = $userRow['avatar'];
    $locatie = $userRow['locatie'];
    $info = $userRow['info'];
    $email = $userRow['email'];
    $username = $userRow['username'];
}

?>

And this is the part where it has to display the rows from the database (the part the user can see when he is on his own profile page)

    <?php if(isset($_SESSION['username'])){ 

?>
        <div class="col-lg-6">
            <h4>Naam:</h4>
            <p><?php echo $username; ?></p>
            <h4>Locatie:</h4>
            <p><?php echo $locatie; ?></p>
            <h4>E-mailadres:</h4>
            <p><?php echo $email; ?></p>
            <h4>Informatie:</h4>
            <p><?php echo $info; ?></p>
                <a href="/editProfile.php">Klik hier</a> om uw profiel te bewerken.
        </div>
        <div class="col-lg-6">
        <?php echo "<img class='useravatar' src='/avatar/user" . $id . ".jpg'></img>"; ?>



    <?php 
    } else {
        echo "U hebt geen bevoegdheid om deze pagina te bekijken";
    }
    ?>

Why does it not show anything?

this is the verify page from the login form:

$query = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'") or die(mysql_error());
$row = mysql_num_rows($query) or die(mysql_error());

if($row == 1){
        $queryFetch = mysql_fetch_array($query);
        session_start();
        $_SESSION['username'] = $queryFetch['username'];
        $_SESSION['role'] = 'user';
        $_SESSION['email'] = $queryFetch['email'];
        $_SESSION['user_id'] = $queryFetch['user_id'];

        if(isset($_SESSION['username'])){
            header ('location: /usercp.php');
        }

    }
}

?>

user3428971
  • 141
  • 1
  • 1
  • 10
  • 1
    You probably meant to use `if(isset($_SESSION['user_id']))` instead of `if(isset($_SESSION['username']))` since you've defined it in `$id = $_SESSION['user_id'];` – Funk Forty Niner May 12 '14 at 22:00
  • Thanks for your reply but that is not the problem when I change that. The $_SESSION['username'] was defined in the page which logs the user in. – user3428971 May 12 '14 at 22:05
  • You're welcome. Is `session_start();` inside all your files? – Funk Forty Niner May 12 '14 at 22:06
  • Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` and use `var_dump();` on your variables. One of which you can do is `var_dump($_SESSION);` to see if anything comes up or not. – Funk Forty Niner May 12 '14 at 22:10
  • It's in the usercp.php (the code shown above) and editprofile.php. But the weird thing is that the username is showing up in the header (also same file (usercp.php)). if(isset($_SESSION['username'])) { echo ""; } but not in the content section – user3428971 May 12 '14 at 22:11
  • Ok, well I don't see nor do I know how you're defining if `$_SESSION['username']` is set or not or how/where it's being assigned as you did for `$id = $_SESSION['user_id'];`. You'll need to provide more or full code. Do you not have something like `$username=$_SESSION['username'];` or something similar, or coming from a POST variable from a form? – Funk Forty Niner May 12 '14 at 22:15
  • This is where I think I defined it (that part worked so I guess it's okay). It is the verify.php from the login box: see the edit in main post – user3428971 May 12 '14 at 22:16
  • It's coming from a POST indeed – user3428971 May 12 '14 at 22:18
  • **Woah...** you're mixing your MySQL APIs, they do not mix. You can't use `mysql_` and `mysqli_` functions together, that's your main issue. – Funk Forty Niner May 12 '14 at 22:18
  • Change all instances of `mysql_` to `mysqli_` plus you may have to pass DB connection variables to your query. – Funk Forty Niner May 12 '14 at 22:22
  • I changed everything to mysqli now but it still doesn't work. Will put the errors on and see what it says because it doesn't display any errors atm. Just echo'ing nothing on the places where it should – user3428971 May 12 '14 at 22:22
  • Is your DB connection also `mysql_` based or `mysqli_`? – Funk Forty Niner May 12 '14 at 22:22
  • I'm not sure. This one is online and I'm used to use mysqli_ on WAMP. But things like register user are working fine – user3428971 May 12 '14 at 22:23
  • You need to make sure that everything is `mysqli_` and not `mysql_` then start using `var_dump();` on your variables along with the error reporting codes I've given you above. – Funk Forty Niner May 12 '14 at 22:24
  • Thanks again. Weird when I just changed all the mysql statements to mysqli it gave an mysqli_error which didn't show up before (the connect.php was also in mysqli all the time) Now everything back to mysql and my editCP.php page (to update the user fields) doesn't work anymore. I will go further into this tomorrow. – user3428971 May 12 '14 at 22:35
  • You're welcome. Take it one step at a time and try and setup a similar script at a smaller scale; that helps a lot. – Funk Forty Niner May 12 '14 at 22:36

0 Answers0