xxreenaxx1 Posted January 31, 2011 Share Posted January 31, 2011 Hi I have created a table containing users. Now they can log in. and I want to use the username and display a welcome message. How do I display that? <H1> Welcome Ms <?php echo $myusername; ?> </H1> If I confused you then please let me know Quote Link to comment https://forums.phpfreaks.com/topic/226243-how-do-i-disaply-a-person-name-from-the-database/ Share on other sites More sharing options...
atrum Posted January 31, 2011 Share Posted January 31, 2011 1. When they have logged in grab their name from the db table and assign it to a session id of name. 2. place on your page in the area you wish to display their name and echo that session ID. If you need something more detailed or spell out then that, then please provide your database table structure and I can make an example for ya. Quote Link to comment https://forums.phpfreaks.com/topic/226243-how-do-i-disaply-a-person-name-from-the-database/#findComment-1167876 Share on other sites More sharing options...
Maq Posted January 31, 2011 Share Posted January 31, 2011 You must connect to the database first. Please read the manual on mysql_connect. There are plenty of examples for you to code from. Quote Link to comment https://forums.phpfreaks.com/topic/226243-how-do-i-disaply-a-person-name-from-the-database/#findComment-1167879 Share on other sites More sharing options...
xxreenaxx1 Posted January 31, 2011 Author Share Posted January 31, 2011 I have connected and be able to log in. Now Yes I did grab the username and try to place it on the page. Nothing display. Here is my table CREATE TABLE IF NOT EXISTS `user` ( `Use_ID` int(11) NOT NULL AUTO_INCREMENT, `Use_Name` varchar(20) NOT NULL, `Use_Password` varchar(20) NOT NULL, `Rol_ID` int(11) NOT NULL, PRIMARY KEY (`Use_ID`), Foreign KEY `Rol_ID` (`Rol_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="Examination"; // Database name $tbl_name="User"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE Use_Name='$myusername' and Use_Password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> <? session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> <html> <head> <title>Multiple choice</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> <H1> Welcome Ms <?php echo $myusername; ?> </H1> </body> <noframes><body bgcolor="#FFFFFF" text="#000000"> </body></noframes> </html> Quote Link to comment https://forums.phpfreaks.com/topic/226243-how-do-i-disaply-a-person-name-from-the-database/#findComment-1167883 Share on other sites More sharing options...
atrum Posted January 31, 2011 Share Posted January 31, 2011 Normally when a user logs in and you want to display their name somewhere, you want to use the data pulled from the database rather than a form post. while($r = mysql_fetch_assoc($result){ $_SESSION['myusername'] = $r['Use_Name']; } Add that in below where your registering your session myusername. Then on your html page echo $_SESSION['myusername']; Quote Link to comment https://forums.phpfreaks.com/topic/226243-how-do-i-disaply-a-person-name-from-the-database/#findComment-1167886 Share on other sites More sharing options...
Pikachu2000 Posted January 31, 2011 Share Posted January 31, 2011 The tutorial you've followed appears to be from phpeasystep.com, and like every tutorial I've seen on that site, is obsolete. session_register() is deprecated, and stripslashes() shouldn't be arbitrarily applied to form data. You'd be better served to find a current tutorial and learn the right way first, rather than relearn it later. Quote Link to comment https://forums.phpfreaks.com/topic/226243-how-do-i-disaply-a-person-name-from-the-database/#findComment-1167919 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.