almystersv Posted January 7, 2008 Share Posted January 7, 2008 Hi Guys, I am currently using sessions on all of my pages using the username field. But how do I retrieve the empID of the person logged in?! my session code is <?php session_start(); if (isset($_SESSION['username']) == false){ header("Location: login.php"); exit(); } require "connect.php"; $query = "select * from employee where username = '".$_SESSION['username']."'"; $result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query"); ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/ Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 Use a mysql_fetch command and put it into an array. Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432751 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 any chance of elaborating on that please... Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432754 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 You're not pulling the ID from the Session, you are pulling it from the Database with your Query command. What are you doing after this line? $result = mysql_query($query, $connection) or die ("Unable to perform query $query"); Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432756 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 This is what follows that... It is a form asking the user to fill in the details of the meeting room they wish to book, i also want it to write into the table their empID which is not requested on the form but want it to be carried through to my query page (shown after). <body> <?php include ("header.php"); ?> <h2>Book a Meeting Room</h2> <h3> <form action="MeetingQuery.php" method="get"> <table width="99%" border="0"> <tr> <td width="29%">Select a building:</td> <td width="34%"><select name="building"> <option >[select a Building]</option> <option >North</option> <option >South</option> </select></td> <td width="22%"> </td> <td width="15%"> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Select a Meeting Room: </td> <td><select name="roomID"> <option >[select Meeting Room Number]</option> <option >020</option> <option >021</option> <option >105</option> <option >106</option> <option >113</option> <option >215</option> <option >216</option> </select></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Select a Date: </td> <td><p><script>DateInput('bkgDate', true, 'DD-MON-YYYY')</script></p></td> <td>Select a Time: </td> <td><select name="bkgTime"> <option >[select Meeting Time]</option> <option >0900-1000</option> <option >1000-1100</option> <option >1100-1200</option> <option >1200-1300</option> <option >1300-1400</option> <option >1400-1500</option> <option >1500-1600</option> <option >1600-1700</option> </select></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><?php if(isset($_GET['message1'])) { echo $_GET['message1']; } else if(isset($_GET['message2'])) { echo $_GET['message2']; } else if(isset($_GET['message3'])) { echo $_GET['message3']; } ?></td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><input name="empID" value="<?=$_SESSION['empID']?>" /></td> <td> </td> <td><input name="Save" type="submit" value="Submit Meeting Room" /></td> <td> </td> </tr> </table> </form> </body> </html> QUERY PAGE <?php require "connect.php"; $roomID = $_GET['roomID']; $building = $_GET['building']; $bkgDate = $_GET['bkgDate']; $empID = $_GET['empID']; if($building == "[select a Building]") { $message1 = "Please select a building you wish to have the meeting in"; header("Location: MeetingMain.php?message1=$message1"); exit(); } if($roomID == "[select Meeting Room Number]") { $message2 = "Please select a Room Number"; header("Location: MeetingMain.php?message2=$message2"); exit(); } if($bkgTime == "[select Meeting Time]") { $message3 = "Please select a Meeting Time"; header("Location: MeetingMain.php?message3=$message3"); exit(); } else if($building == !null || $roomID == !null) { $query = "insert into booking ('','".$roomID."','".$empID."','".$bkgDate."','".$bkgTime."')"; $result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query"); //$message7 = "'".$fName."' '".$sName."' added successfully. "; //header("Location: UserADD.php?message7=$message7"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432760 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 So you are really not doing anything with that query. http://us3.php.net/mysql_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432763 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 Will that auto fill the empID in the table with the empID of the person currently logged in? Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432781 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 ...sorry i meant will that auto fill the text box in the form with the empID of the person currently logged in!?? Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432784 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 How are you storing the username now into a session? You just have to do the same for the userid. Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432786 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 I've shown u my session code. thats all im using!? Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432793 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 This is all you've shown if (isset($_SESSION['username']) == false) Where do you assign $_SESSION['username']? It's probably in your login.php file Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432794 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 Ye, my session is declared in my logincheck.php page. This is it... <?php session_start(); require "connect.php"; $username = $_POST['username']; $password = $_POST['password']; $query = "select * from employee WHERE active ='y' AND username='".$username."' AND password='".$password."'"; $result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query"); $row=mysql_fetch_array($result); if($row!=null) { $_SESSION['username'] = $row['username']; header("Location: welcome.php"); exit(); } else if($username == "") { $message2 = "Please enter your Username"; header("Location: login.php?message2=$message2"); exit(); } else if($password == "") { $message3 = "Please enter your Password"; header("Location: login.php?message3=$message3"); exit(); } /*else if(active = "n") { $message4 = "Sorry, this users account has been de-activated. Please contact the system administrator." header("Location: login.php?message4=$message4"); exit(); }*/ else { $message = "Invalid user name or password, please try again"; header("Location: login.php?message=$message"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432800 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 $_SESSION['username'] = $row['username']; header("Location: welcome.php"); exit(); to $_SESSION['username'] = $row['username']; $_SESSION['empID'] = $row['empID']; header("Location: welcome.php"); exit(); Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432804 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 Just changed that, thank you. Now what do i modify on this to auto fill the text box on the meeting room booking form? <input name="empID" value="<?=$_SESSION['empID']?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432809 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 If short tags are enabled, then nothing. But if not, change to <input name="empID" value="<?php echo $_SESSION['empID']?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432811 Share on other sites More sharing options...
almystersv Posted January 7, 2008 Author Share Posted January 7, 2008 Thank you very much for your time! Quote Link to comment https://forums.phpfreaks.com/topic/84888-solved-getting-variables-from-a-session/#findComment-432817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.