rdkd1970 Posted May 14, 2011 Share Posted May 14, 2011 Can someone help me I have been working on this problem for some time. I have coded my page to welcome, firstname. At first in my insert I had used the id I have not switched it to the username as id was including everyone of my test names in the welcome. It looks like the sessions are continuing as I am able to go to the private section of the pages. However I am not sure what I have to do to produce the welcome, firstname to go to all the pages and let me know it is allowing the one member to access the private sessions. Also when I use the login form it does not recognize the users. It is like the connection to the database dies i guess. here is my codes. <?php session_start(); ini_set ("display_errors", "1"); error_reporting(E_ALL); ?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome</title> <style type="text/css"> .background {color: #B56AFF; } </style> </head> <body> <p> <?php /* Program: login.php * Desc: Displays the new member welcome page. Greets * member by name and gives a choice to enter * restricted section or go back to main page. */ if (isset($_SESSION['username'])) { // Set the users session ID include("Connections/connect_to_mysql.php"); //Formulate Query //This is the best way to perform an SQL query $query = mysql_query ("SELECT * FROM `Members` WHERE username={$_SESSION['username']}"); $result = mysql_query($query); $numrows = mysql_num_rows($query); //Check result //This shows the actual query sent to MySQL and the error. Useful for debugging. if(!$result){ $message = 'Invalid query:' . mysql_error() . "\n"; $message .= 'Whole query:' . $query; die($message); } //Use result //Attempting to print $result won't allow access to information in the resource //One of the mysql result functions must be used //See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. if($numrows!=0){ while($row = mysql_fetch_assoc($query)); $dbusername = $row['username']; $dbpassword = $row['password']; //check to see if they match! if($username==$dbusername&&md5($password)==$dbpassword){ echo "Welcome, ".$_SESSION['firstname']. "!<br><a href='logout_test.php'>Logout</a>"; } } } ?> </p> <p> </p> <p>Your new Member accounts lets you enter the members only section of our web site. You'll find special discounts, a profile of matches, live advise from experts, and much more.</p> <p>Your new Member ID and password were emailed to you. Store them carefully for future use.</p> <div style="text-align: center"> <p style="margin-top: .5in; font-weight: bold"> Glad you could join us!</p> <form action="profile.php" method="post"> <input type="submit" value="Enter the Members Only Section"> </form> <form action="index.php" method="post"> <input type="submit" value="Go to Main Page"> </form> </div> </body> </html> <?php session_start(); ini_set ("display_errors", "1"); error_reporting(E_ALL); ?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome</title> <style type="text/css"> .background {color: #B56AFF; } </style> </head> <body> <p> <?php /* Program: login.php * Desc: Displays the new member welcome page. Greets * member by name and gives a choice to enter * restricted section or go back to main page. */ if (isset($_SESSION['username'])) { // Set the users session ID include("Connections/connect_to_mysql.php"); //Formulate Query //This is the best way to perform an SQL query $query = mysql_query ("SELECT * FROM `Members` WHERE username={$_SESSION['username']}"); $result = mysql_query($query); $numrows = mysql_num_rows($query); //Check result //This shows the actual query sent to MySQL and the error. Useful for debugging. if(!$result){ $message = 'Invalid query:' . mysql_error() . "\n"; $message .= 'Whole query:' . $query; die($message); } //Use result //Attempting to print $result won't allow access to information in the resource //One of the mysql result functions must be used //See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. if($numrows!=0){ while($row = mysql_fetch_assoc($query)); $dbusername = $row['username']; $dbpassword = $row['password']; //check to see if they match! if($username==$dbusername&&md5($password)==$dbpassword){ echo "Welcome, ".$_SESSION['firstname']. "!<br><a href='logout_test.php'>Logout</a>"; } } } ?> :'( Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/ Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 //Formulate Query //This is the best way to perform an SQL query $query = mysql_query ("SELECT * FROM `Members` WHERE username={$_SESSION['username']}"); $result = mysql_query($query); $numrows = mysql_num_rows($query); You are calling mysql_query twice. First to execute the query and then again to query the same query you created; which won't work. Change it to this. //Formulate Query //This is the best way to perform an SQL query $sql= "SELECT * FROM `Members` WHERE username={$_SESSION['username']}"; $query = mysql_query($sql); $numrows = mysql_num_rows($query); Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215331 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 that created an error message but i did get the welcome to everyone of my tested members. Could it be my while loop and the numrows something i did wrong. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215336 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 I am not sure how to get the if statement to produce just one name the name I enter at the form. Some reason this is showing everyone. if($numrows!=0){ while($row=mysql_fetch_assoc($result)){ echo "Welcome, {$row['firstname']}"; } Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215340 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 An the error message was.. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215341 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 This was the error message. Notice: Undefined index: username in /home/ebermy5/public_html/login.php on line 34 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ebermy5/public_html/login.php on line 36 Notice: Undefined variable: result in /home/ebermy5/public_html/login.php on line 41 Invalid query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Whole query: Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215345 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 if (isset($_SESSION['username'])) { You check for this variable but I don't see where you actually declared it. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215347 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 I changed this part and get all users welcome no error messages. I updated users in the insert to id I thought that would single out the members.???? if (isset($_SESSION['id'])) { // Set the users session ID $id=$_SESSION['id']; Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215348 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 I updated users in the insert to id I thought that would single out the members.???? It should, perhaps you don't have auto-increment checked in your id field... in which case you probably don't have a primary key set either. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215349 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 they are set I rechecked a few times. Should I speak to my hosting. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215351 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 They will just tell me it is my coding to recheck.???? Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215352 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 Well, if it's displaying every user, then you obviously have duplicate ids somewhere in your table. I can't think of any other reason why it would show every user when you are restricting it to an id that you claim is both auto increment and a primary key. Talking to your host won't help anything, it might just make it worse. Something is defunct in your code and I can't seem to spot it. All I can really say is to go check your database and see if there are duplicates. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215354 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 this is what I have I. Is the email field better to remove I can't remember why I made it like this. Action Keyname Type Unique Packed Field Cardinality Collation Null Comment PRIMARY BTREE Yes No id 191 A email BTREE Yes No email 191 A Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215359 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 Can you post the actual data in the database rather than an EXPLAIN Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215361 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 I am not sure if you mean this. Field Type Collation Attributes Null Default Extra Action id int(20) No None auto_increment username varchar(100) latin1_swedish_ci No None firstname varchar(100) latin1_swedish_ci No None lastname varchar(100) latin1_swedish_ci No None email varchar(255) latin1_swedish_ci No None password varchar(100) latin1_swedish_ci No None Members Field Type Null Default Comments id int(20) No username varchar(100) No firstname varchar(100) No lastname varchar(100) No email varchar(255) No password varchar(100) No Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215362 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 No, that's not it either. Try clicking Browse. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215363 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 There is about 7 pages of testing here are the last group of names id username firstname lastname email password 182 Peanuts Fred Trott peanuts@fred.com 6d42f9acbb25c12ace0208d7d6bd9809 0000-00-00 183 Slick Roger Smith slick@roger.com 71d8d973e2bd9bc7b220c4df75c06dd4 0000-00-00 184 Cupcake Kathy Wilkinson cupcake@kathy.com 7bb06121c41632bcf7c69f88078f3402 0000-00-00 L 185 Takelonger Peter Wilcox takelonger@peter.com 525c5db35814592cba76eb5bc0c018d3 0000-00-00 186 Jahsyi Jahsyi Turini jahsyi@turini.com 0ce341d37457bb13e53773ebe8d437c1 Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215364 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 If it's showing all the users, then it has to be your query. Furthermore, since you only expect ONE result... why use a loop? The loop is creating all the names, but it will only loop through what your query retrieves so you must have some query issues. Post your revised code and let's see if we can't spot it this time. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215366 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 I had checked the if statement This code echos everyone in the db if($numrows!=0){ while($row=mysql_fetch_assoc($result)){ echo "Welcome, {$row['firstname']}"; } this one does not echo anyone if($numrows<1){ while($row=mysql_fetch_assoc($result)){ echo "Welcome, {$row['firstname']}"; } this one echos everyone. if($numrows=1){ while($row=mysql_fetch_assoc($result)){ echo "Welcome, {$row['firstname']}"; } Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215368 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 post your revised query, echo the $sql variable if there is one.... there should be if there's not. Neither of those examples you showed should make the script echo every user in the table. The only problem with the last one is that you're declaring $numrows to 1 instead of comparing it. Otherwise, they should all work fine. It's the query that's getting all the users, it's the only possible reason. Either post the revised code or post the echoed query. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215370 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 This is my revised query that gets all the members in the query. */ if (isset($_SESSION['id'])) { // Set the users session ID $id=$_SESSION['id']; include("Connections/connect_to_mysql.php"); //Formulate Query //This is the best way to perform an SQL query $query = "SELECT id, firstname FROM `Members` WHERE id={$_SESSION['id']}"; $result = mysql_query($query); $numrows = mysql_num_rows($result); //Check result //This shows the actual query sent to MySQL and the error. Useful for debugging. if(!$result){ $message = 'Invalid query:' . mysql_error() . "\n"; $message .= 'Whole query:' . $query; die($message);} //Use result //Attempting to print $result won't allow access to information in the resource //One of the mysql result functions must be used //See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. if($numrows!=0){ while($row=mysql_fetch_assoc($result)){ echo "Welcome, {$row['firstname']}"; } //Free the resources associated with the result set mysql_free_result($result); } Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215374 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 According to your code, there is no reason for it to show all users. Even if the id in the SESSION was incorrect, you should still get either an error or the wrong result. There is a fix to this, but it will not answer this mind boggling question. On your query... add LIMIT 1 to the end. //Formulate Query //This is the best way to perform an SQL query $query = "SELECT id, firstname FROM `Members` WHERE id={$_SESSION['id']} LIMIT 1"; Then you're sure to only get one result, but like I said... this is, how you say... afro-engineered. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215379 Share on other sites More sharing options...
rdkd1970 Posted May 14, 2011 Author Share Posted May 14, 2011 Boy this is some puzzle and the sad thing is it is something so easy but I am missing it somewhere. my error message. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ebermy5/public_html/login.php on line 36 Invalid query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 Whole query:SELECT id, firstname FROM `Members` WHERE id=idLIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215385 Share on other sites More sharing options...
wildteen88 Posted May 14, 2011 Share Posted May 14, 2011 Make sure you have typed your query exactly as Zanus posted. With that error it suggestes you havn't typed the query in correctly you have forgotten to type a space before the LIMIT keyword Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215387 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 SELECT id, firstname FROM `Members` WHERE id=idLIMIT 1 There is no space between id and LIMIT. Also, the fact that you have id=id in your query answers this puzzle. Essentially, that query says Get all users whose id is equal to their id... which is all of them. So now I'm back to my second post on this thread.. or third I can't remember. You never set $_SESSION['id'] to contain an actual id. Once you do that, you should have a working query... you won't even need that LIMIT duct tape. Quote Link to comment https://forums.phpfreaks.com/topic/236396-session-continues-but-does-not-echo-members/#findComment-1215389 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.