derekbelcher Posted March 18, 2009 Share Posted March 18, 2009 I have a database that contains usernames, passwords, and first and last names associated with each username. I want to display the users first and last name when they are logged in. The code that I am using to pull this information is: <td height="19" colspan="3" valign="top">Welcome <b><?php $sql = mysql_query("SELECT * FROM `officers`"); $fetch = mysql_fetch_assoc($sql); $firstname = $fetch['firstname']; $lastname = $fetch['lastname']; echo $firstname , $lastname; ?></b></td> The table is "officers". The problem is 1) I only get the first and last name of the first person in the table regardless of the login, and The first and last name have no space between them when displayed. How do I specify that the first and last name should be associated with the username that was used to log in? Thanks. Quote Link to comment Share on other sites More sharing options...
stevehossy Posted March 18, 2009 Share Posted March 18, 2009 ("SELECT * FROM `officers` WHERE username=postedusername"); ? your query just wants to get data from ever user in officers table. you need to limit it by putting WHERE the username or password equals the posted pass or username in login page. Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 O.K. I know this is going to sound stupid, but i am very new and learning here. You said to add WHERE username=postedusername. What am i supposed to put in the "posted username" Is there a certain variable from my table, or something. my column headings are, ID, username, password, firstname, lastname. I tried WHERE username=username, but that didn't work. Sorry for the "stupidity"..Thanks Quote Link to comment Share on other sites More sharing options...
stevehossy Posted March 18, 2009 Share Posted March 18, 2009 The form where the user logins. Say the input name for the password box is password. YOu would put where password='{$_POST['password']}' so it should be mysql_query("SELECT * FROM `officers` WHERE password='{$_POST['password']}' "); Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 I'm sure I am getting close here, but it doesn't display anything. On the login page, the input is myusername. So I did this: <?php $sql = mysql_query("SELECT * FROM `officers` WHERE username='{$_POST['myusername']}' "); $fetch = mysql_fetch_assoc($sql); $firstname = $fetch['firstname']; $lastname = $fetch['lastname']; echo $firstname , $lastname; ?> But the display on the logged in page just says Welcome. No name is shown. My login.php page sends the info to a checklogin.php page and if the username and password check out then the user sees the right page, if not they get an error page. I am at a loss. Any other ideas? Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 Still not sure what I am missing here. Anyone have any ideas? ??? Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted March 18, 2009 Share Posted March 18, 2009 <?PHP $firstname = $_POST['firstname'] ; $lastname = $_POST['lastname'] ; $user = $_POST['myusername'] ' $sql = "SELECT * FROM officers WHERE username LIKE myusername" ; $result = mysql_query($sql) ; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $dbfname = $row['firstname'] ; $dblname = $row['lastname'] ; $dbuser = $row['username'] ; } echo "$dbfname , $dblname" ; ?> right? Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 This all makes since, but when I put the code in, I get: Welcome Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/athens/public_html/Officers Area/officer_index.php on line 72 , Was there anything in the code above that I should have changed to be specific with my database? This all seems to make since, but I am not sure how to trouble shoot errors like the one above. Thanks for your help!!! Quote Link to comment Share on other sites More sharing options...
tracivia Posted March 18, 2009 Share Posted March 18, 2009 This error mostly means, your query returns an error or empty set. require_once('connect_script.php'); //or just put mysql_connect and mysql_select_db $un = trim($_POST['username']); $pw = trim($_POST['password']); $query = 'SELECT * FROM users WHERE username=\'' . $un . '\' AND password=\'' . $pw . '\''; //You will mostly get one result $rs = mysql_query($query); $u_obj = mysql_fetch_object($rs); print 'Hi ' . $u_obj->first_name . ' ' . $u_obj->last_name . ', i am a high tech website that can say hi and your name'; I hope these make since Notice the single quotes in the query. Don't forget t Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 The problem in my code appears to be with the: $sql = "SELECT * FROM `officers`" If I enter this line, then I get an output of the last row in the table regardless of what username is used to login. When I try to add the WHERE information I get an error. I am really not sure what I need to do to fix this. I tried to use: WHERE username = $_POST['myusername'], but that didn't work either. I would be happy to email the code to someone if that would help. Thanks. Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 I am still searching for an answer here. Just in case anyone happened by this post. No Rush...Thanks for everyone's help...this is an awesome resource Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 O.K. I am learning here...I think I understand a little more about what my code is doing: In my SELECT * FROM `TABLE NAME` WHERE username (that is the column heading) = something. This is where I am getting stuck. I want username to = all the names in that column. Then I want some code that checks to see which user is logged in (based on username) and displays the real name associated with that username. In my table, I have a column that has usernames (e.g. dbelcher) and for dbelcher, on the same line, there is a first name listed under the column heading firstname. Does this make any since? Quote Link to comment Share on other sites More sharing options...
derekbelcher Posted March 18, 2009 Author Share Posted March 18, 2009 I did it!!! Thanks for all your help. I used the following: $sql = "SELECT * FROM `officers` WHERE username = '{$_SESSION['myusername']}'" It was the Session that was throwing me off... Thanks again! Quote Link to comment 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.