Jump to content

Recommended Posts

hello. i am having a problem i think has to do with $_SESSION. when i load the member.php page after the login page, a mysql query is run and returns the users 'friends' and displays then in a list as well as the total number of friends. this is working perfectly. but when i leave this page and come back to it, it says the user has 0 friends. i cannot figure out why the information is either lost, or why the query does not run again to select the friends unless they enter the member.php page from the login page. while on the member.php page if you click the 'home' link, it points to the member.php page, but does not retain the correct query results. following is my member.php page (which mostly calls other functions in external files) as well as the query function that selects the friends. any help you can give to set me in the right direction is appreciated.

i am pretty new to php and got most of this code from the book 'PHP and MySQL Web Development' from Developer's Library and am trying to make it work for my personal needs.

---------------------

member.php

--------------------

<?php

 

// include function files for this application

require_once('login_fns.php');

session_start();

 

//create short variable names

$username = $_POST['username'];

$passwd = $_POST['passwd'];

 

if ($username && $passwd) {

// they have just tried logging in

  try  {

    login($username, $passwd);

    // if they are in the database register the user id

    $_SESSION['valid_user'] = $username;

  }

  catch(Exception $e)  {

    // unsuccessful login

    do_html_header('Problem:');

    echo 'You could not be logged in.

          You must be logged in to view this page.';

    do_html_url('login.php', 'Login');

    do_html_footer();

    exit;

  }

}

 

do_html_header('Home');

 

check_valid_user();

 

get_friends($username);

 

display_user_menu();

 

do_html_footer();

 

?>

----------------------

get friends function

----------------------

function get_friends($username) {

 

$conn = db_connect();

$result = $conn->query("select friendname FROM friends WHERE username='".$username."'");

 

$friends = $result;

 

$num_results = $result->num_rows;

 

if ($num_results == 0) {

    echo "</br>";

    echo "You do not have any friends. Loser.";

  } else {

 

echo "<p>Number of friends: ".$num_results."</p>";

 

for ($i=0; $i < $num_results; $i++) {

$row = $friends->fetch_assoc();

echo "<br />";

echo $row['friendname'];

}

}

}

Link to comment
https://forums.phpfreaks.com/topic/201052-losing-query-results-when-page-reloads/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.