Jump to content

Recommended Posts

Hello All,

 

I'm new to PHP, and I've adapted a previous login script for my needs, and that works great. The login member-id is stored in the session. My question is how do I use that ID stored in the session to be used for a quarry. Here is the code:

 

<?php
// Start a session
session_start();

// Sends the user to the login-page if not logged in
if(!session_is_registered('member_ID')) :
header('Location: index.php?msg=requires_login');
endif;

// Include database information and connectivity
include 'db.php';

// We store all our functions in one file
include 'functions.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Members Only</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Welcome <?php print user_info('username'); ?></p>
<p>Welcome <?php print user_info('user_password'); ?></p>
<p>Welcome <?php print user_info('firstname'); ?> <?php print user_info('lastname'); ?></p>
<p><a href="logout.php" title="Log out of the member area">Log out</a></p>
</body>
</html>
<?php
$data = mysql_query("SELECT * FROM iba WHERE studentid='INSERT SESSION MEMBER ID HERE'")
or die(mysql_error(Oops));
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['studentid'] . "</td> ";
Print "<th>Color:</th> <td>".$info['date'] . "</td> ";
Print "<th>Food:</th> <td>".$info['event'] . "</td> ";
Print "<th>Pet:</th> <td>".$info['ammount'] . " </td></tr>";
}
Print "</table>";
?>

 

Note where is says "INSERT SESSION MEMBER ID HERE"

 

Basically, I have two tables in my database. One is used for logging in, the other is used to store information specific to the user. Once logged in, the script uses the session member id to verify that the user is logged in. I want that same member id to be used in a quarry to draw information about that user from the second table.

 

Does that make sense? Thanks guys!

 

 

EDIT: Note that some of the code was just used to practice drawing information from the database. For example, the end product won't list the users password. Etc...

Link to comment
https://forums.phpfreaks.com/topic/156068-solved-insert-session-value-into-quarry/
Share on other sites

If I understand this correctly, one table holds the login information, so the second table should hold some reference to that person's id number or login name, etc.  There has to be some cross between the tables, a common field or reference to the other table.

 

If I am not mistaken, you could query the login id table for the current value of their student id or whatever common field between the two tables is to reference the second and store that in a variable, for example "$othermysqlqueryresult"

 

Then you code would look something like:

$data = mysql_query("SELECT * FROM iba WHERE studentid=$othermysqlqueryresult")

Well, I actually solved it myself. I took another suggestion from an entirely different subject I randomly found while browsing the these threads. It's amazing, I've tried for three days to figure this out, have posted the same thread on about 5 PHP help forums, and I find the right answer by skimming through something totally unrelated. :-) It's crazy.

 

In case you're interested, I used this:

$username = $_SESSION["member_ID"];

 

Then did my quarry as:

$data = mysql_query("SELECT * FROM iba WHERE studentid ='$username'") 

 

Works like a charm!!

 

 

Here's the final code:

<?php
// Start a session
session_start();

// Sends the user to the login-page if not logged in
if(!session_is_registered('member_ID')) :
header('Location: index.php?msg=requires_login');
endif;

// Include database information and connectivity
include 'db.php';

// We store all our functions in one file
include 'functions.php';
$username = $_SESSION["member_ID"];
$data = mysql_query("SELECT * FROM iba WHERE studentid ='$username'") 
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Student ID:</th> <td>".$info['studentid'] . "</td> ";
Print "<th>Date:</th> <td>".$info['date'] . "</td> ";
Print "<th>DEP/WTD:</th> <td>".$info['dep/wtd'] . "</td> ";
Print "<th>Event:</th> <td>".$info['event'] . "</td> ";
Print "<th>Ammount:</th> <td>".$info['ammount'] . "</td> ";
Print "<th>Total:</th> <td>".$info['total'] . " </td></tr>";
}
Print "</table>";
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Members Only</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p><?php print user_info('firstname'); ?> <?php print user_info('lastname'); ?> (Student ID: <?php print user_info('username'); ?>)</p>
    <p>Welcome <?php print user_info('user_password'); ?></p>
    <p>Welcome <?php print user_info('firstname'); ?> <?php print user_info('lastname'); ?></p>
<p><a href="logout.php" title="Log out of the member area">Log out</a></p>
</body>
</html>

 

If anyone sees and problems or weaknesses doing it this way, please let me know!! But I'm happy that it finally works the way I want it to! :-)

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.