Shoe Posted December 23, 2010 Share Posted December 23, 2010 Ok so I pretty much just started at .php, and am constructing a registration page/login page for my website that hooks up to MySQL database. All the other parts of my code work except for this file, the login.php file. This is pretty much the whole code, leaving out my database login/password: //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); session_start(); $username = $_POST[‘username’]; $password = $_POST[‘password’]; $query = “select * from users where username=’$username’ and password=’$password’”; $result = mysql_query($query); if (mysql_num_rows($result) != 1) { $error = “Bad Login”; include “login.html”; } else { $_SESSION[‘username’] = “$username”; include “memberspage.php”; } ?> The error message I receive when running the .php script is: Parse error: syntax error, unexpected T_STRING in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 19 And line 19 is: $query = “select * from users where username=’$username’ and password=’$password’”; I've tried almost everything to fix this line of code and finally came to you guys. What is wrong with it? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/ Share on other sites More sharing options...
QuickOldCar Posted December 23, 2010 Share Posted December 23, 2010 mysql_query missing //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); session_start(); $username = $_POST[‘username’]; $password = $_POST[‘password’]; $query = mysql_query(“select * from users where username=’$username’ and password=’$password’”); $result = mysql_query($query); if (mysql_num_rows($result) != 1) { $error = “Bad Login”; include “login.html”; } else { $_SESSION[‘username’] = “$username”; include “memberspage.php”; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150617 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Hm, still getting the exact same error. Still an unexpected T_STRING in line 19. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150619 Share on other sites More sharing options...
Pikachu2000 Posted December 23, 2010 Share Posted December 23, 2010 You're using the wrong quotes. Those are curly or 'smart' quotes. You need to use straight quotes for both single and double quoting. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150620 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Ah. That was it. Thanks a million. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150621 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Alright (really sorry to bother you guys with my lack of .php knowledge, im sure this stuff is easily repairable) so now the login screen is showing up (yay!) but I am getting two errors. #1: Warning: mysql_query() expects parameter 1 to be string, resource given in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 21 # 2: Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 23 Any idea what to do? Thanks guys, I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150622 Share on other sites More sharing options...
Pikachu2000 Posted December 23, 2010 Share Posted December 23, 2010 You're attempting to execute a query twice. Once here: $query = mysql_query("select * from users where username='$username' and password='$password'"); then again here, using the result resource from the first query as a query string: $result = mysql_query($query); So, that part needs to be rewritten as: $query = "select * from users where username='$username' and password='$password'"; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150630 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Alright, thank you very much. So now I got all of the error messages deleted, but when I run the login.php file, it links to the memberspage.php without having the user login first. In other words, it just displays "Welcome, !" instead of displaying a login form. Here is the memberspage.php, although I'm not sure if it will be of use to you. <?php session_start(); echo "Welcome, ".$_SESSION['username']."!"; ?> Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150634 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 23, 2010 Share Posted December 23, 2010 Alright, thank you very much. So now I got all of the error messages deleted, but when I run the login.php file, it links to the memberspage.php without having the user login first. In other words, it just displays "Welcome, !" instead of displaying a login form. Here is the memberspage.php, although I'm not sure if it will be of use to you. <?php session_start(); echo "Welcome, ".$_SESSION['username']."!"; ?> Thanks guys. change it to this code <?php //starts session session_start(); //check if logged-in if not echo login form if(!session_is_registered(username)){ echo "<form action='login.php' method='post' name='login'> Username:<input name='username' type='text' value=''> Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'> </form>"; } // if user logged in, then echo welcome, username else { echo "<center> Welcome, "; echo($_SESSION['username']); echo "</center>" } ?> if you want to the user to be redirected to the previous page if not logged in, the change echo "<form action='login.php' method='post' name='login'> Username:<input name='username' type='text' value=''> Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'> </form>"; with header("location:index.php"); Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150663 Share on other sites More sharing options...
PFMaBiSmAd Posted December 23, 2010 Share Posted December 23, 2010 session_is_registered() was depreciated over 8 years ago when the $_SESSION variable was introduced. You would use isset() to test if a $_SESSION variable is set or not. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150665 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 23, 2010 Share Posted December 23, 2010 ok. then change //check if logged-in if not echo login form if(!session_is_registered(username)){ to //check if logged-in if not echo login form if(!isset(username)){ Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150666 Share on other sites More sharing options...
mmarif4u Posted December 23, 2010 Share Posted December 23, 2010 And how about this: if(!isset($_SESSION['username'])){ Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150667 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 23, 2010 Share Posted December 23, 2010 ok, use this instead if(!isset($_SESSION['username'])){ Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150668 Share on other sites More sharing options...
mmarif4u Posted December 23, 2010 Share Posted December 23, 2010 <?php //starts session -- no white spaces -- session_start(); //check if logged-in if not echo login form if(!isset($_SESSION['username'])){ echo "<form action='login.php' method='post' name='login'> Username:<input name='username' type='text' value=''> Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'> </form>"; } // if user logged in, then echo welcome, username else { echo "<center>Welcome, <strong>".$_SESSION['username']."</strong></center>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150669 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Alright, fixed the memberspage.php (I think) but now when I run the login.php, I get a random error saying: Parse error: syntax error, unexpected '}', expecting ',' or ';' in /Applications/XAMPP/xamppfiles/htdocs/memberspage.php on line 23 Line 23: $error = "Bad Login" Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150790 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Oh wait, the error is in the memberspage.php I'm stupid.. Ok so I used the code you guys gave me for the memberspage.php and I fixed the error I just received (semicolon missing) but now im getting a: Deprecated: Function session_is_registered() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/memberspage.php on line 7 And line 7: if(!session_is_registered(username)){ Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150796 Share on other sites More sharing options...
PFMaBiSmAd Posted December 23, 2010 Share Posted December 23, 2010 The line of code you posted is missing the semi-colon ; at the end of the statement. And that cannot be a random error because it is a fatal parse error and is due to incorrect syntax in the code. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150797 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 The line of code you posted is missing the semi-colon ; at the end of the statement. And that cannot be a random error because it is a fatal parse error and is due to incorrect syntax in the code. Ah sorry, didn't know "random" was a technical term in .php. I was just using it as an adjective Ok so now I've got all the errors out of the way, but when I run login.php I'm still just receiving: "Welcome, " Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150806 Share on other sites More sharing options...
PFMaBiSmAd Posted December 23, 2010 Share Posted December 23, 2010 Programming is an exact science and we only see the information that you provide in your post. Random in programming or in a programming problem means something different than the same error always occur at the same point. I'm still just receiving: "Welcome, " ^^^ It would take seeing your current code that is setting the session variable and the code that is displaying the session variable to be able to help you with why it is not working. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150812 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Programming is an exact science and we only see the information that you provide in your post. Random in programming or in a programming problem means something different than the same error always occur at the same point. I'm still just receiving: "Welcome, " ^^^ It would take seeing your current code that is setting the session variable and the code that is displaying the session variable to be able to help you with why it is not working. Ok so I have four files, two of which deal with the registration phase, two of which deal with the login phase. Then I have the memberspage.php of course. The one login form is just an .html that the login.php links with. The other is the login.php where I think the session variable is set... <?php //Database Information $dbhost = "localhost"; $dbname = "__________"; $dbuser = "__________"; $dbpass = "_______________"; //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); session_start(); $username = $_POST['username']; $password = $_POST['password']; $query = "select * from users where username='$username' and password='$password'"; $result = mysql_query($query); if (mysql_num_rows($result) != 1) { $error = "Bad Login"; include "login.html"; } else { $_SESSION['username'] = "$username"; include "memberspage.php"; } ?> Then if the login is correct, it displays memberspage.php which you guys gave me which now looks like this: <?php //starts session session_start(); //check if logged-in if not echo login form if(!isset($_SESSION['username'])){ echo "<form action='login.php' method='post' name='login'> Username:<input name='username' type='text' value=''> Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'> </form>"; } // if user logged in, then echo welcome, username else { echo "<center> Welcome, "; echo($_SESSION['username']); echo "</center>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150816 Share on other sites More sharing options...
PFMaBiSmAd Posted December 23, 2010 Share Posted December 23, 2010 Your code works for me, other than a notice error concerning using two session_start() statements. About the only way the posted code would NOT display the username is if you have a row in your user table with a blank username and either your form is not submitting data using the fields with those names or you entered nothing in the username field in the form. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150829 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Alright thanks man, I appreciate it. Anything I can do to fix the session_start() statements? Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150832 Share on other sites More sharing options...
PFMaBiSmAd Posted December 23, 2010 Share Posted December 23, 2010 You only use one on any page. The second one wastes processing time executing and in handling the following error - Notice: A session had already been started - ignoring session_start() in ....\memberspage.php on line 4 Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150834 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Hm, still not workin for me. Oh well. Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150837 Share on other sites More sharing options...
Shoe Posted December 23, 2010 Author Share Posted December 23, 2010 Ah. Finally. Got it to work. Thank you all very much, I really appreciate your time! Now one more thing, what could do I put on pages so that they will have to login to access that particular page? Quote Link to comment https://forums.phpfreaks.com/topic/222464-database-code-problem/#findComment-1150857 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.