tberger Posted April 23, 2007 Share Posted April 23, 2007 When someone logs into my application - a login function runs from a pinc file. What is suppose to happen is if the username and password match - it is suppose to assign a session to the username - which is being passed from form and to the first and last name which are in the table for the user. Can I assign a session variable to value in a mysql table? It does properly set the cookie I am assigning on the setCookie line but is is not setting any of the three sessions. Here is the function I am using: session_start(); function getLogin($username, $pass) { $username = trim($_POST['email_address']); $pass = trim($_POST['password']); $db=mysql_connect('monet.homeip.net','conn1','conn1') or die ("Cannot connect to server"); mysql_select_db('test',$db) or die ("Cannot select DB"); // Mysql_num_row is counting table row $result=mysql_query("SELECT * FROM seebergerLogin WHERE username='$username' and password='$pass'"); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched username and password, table row must be 1 row if($count==1){ // Register $username, $pass $_SESSION['username'] = $username; $_SESSION['fName'] = $first_name; $_SESSION['lName'] = $last_name; setcookie ("ok", $username); return TRUE; } else { echo "Wrong Username or Password"; } } ?> I am sure I am missing something simple (at least I hope that is all) I appreciate any help you can offer. TJ Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/ Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 $_SESSION['username'] = $username; should work as for $_SESSION['fName'] = $first_name; $_SESSION['lName'] = $last_name; you need to fetch the data from the database Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/#findComment-236101 Share on other sites More sharing options...
tberger Posted April 23, 2007 Author Share Posted April 23, 2007 Ok so I tried this: if($count==1){ // Register $username, $pass $row = mysql_fetch_array($result) or die(mysql_error()); $_SESSION['username'] = $username; $_SESSION['fName'] = $first_name; $_SESSION['lName'] = $last_name; setcookie ("ok", $username); return TRUE; } else { echo "Wrong Username or Password"; } } On the welcome page where I am calling in these sessions, it is still not displaying the firstname and last name. The username does work in both the session and the setcookie. Here is where it is being called: require('library.pinc'); $cookie= $HTTP_COOKIE_VARS["ok"]; if (!isset($cookie)){ header("Cache-Control: no-cache, must-revalidate"); } echo "Welcome, "; echo $_SESSION[ 'first_name'] . " " . $_SESSION['last_name'], "<br />"; echo "You have successfully logged in as "; echo $_SESSION['username'], "<br />"; echo "Your first and last names were passed by session data; your username was stored in the cookie. ", "<br />"; echo "The cookie value is: "; echo $cookie, "<br />"; I am thinking I am not fetching correctly - any suggestions?? Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/#findComment-236128 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 please use [ code] tags <?php $row = mysql_fetch_array($result) or die(mysql_error()); ?> is correct but you need to set the $first_name & $last_name so for example just say the field name of the first name is first and the field name for the last name is last change <?php $row = mysql_fetch_array($result) or die(mysql_error()); $_SESSION['username'] = $username; $_SESSION['fName'] = $first_name; $_SESSION['lName'] = $last_name; ?> to <?php $row = mysql_fetch_array($result) or die(mysql_error()); $_SESSION['username'] = $username; $_SESSION['fName'] = $row['first']; $_SESSION['lName'] = $row['last']; ?> *note doesn't it look nicer and easier to read in code tags Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/#findComment-236179 Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 edit Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/#findComment-236185 Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 $_SESSION['fName'] = $first_name; $_SESSION['lName'] = $last_name; used to create the sessions.... echo $_SESSION[ 'first_name'] . " " . $_SESSION['last_name'], " used to display the sessions... theyre different variables... Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/#findComment-236191 Share on other sites More sharing options...
tberger Posted April 23, 2007 Author Share Posted April 23, 2007 Brilliant - that did the trick. Thank you so much. Yes - you are correct the code blocks are much easier to read - will use in the future!! Thank you so much for you help!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/#findComment-236196 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 your welcome good luck and happy coding Quote Link to comment https://forums.phpfreaks.com/topic/48296-solved-assign-table-value-as-a-session/#findComment-236203 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.