Skatecrazy1 Posted December 14, 2010 Share Posted December 14, 2010 I know this is probably a missing curly bracket or some other syntax, but I can't seem to spot it, anyone see it? i get this error btw. Parse error: syntax error, unexpected $end in C:\Program Files\xampp\htdocs\cameo\login.php on line 39 <?php session_start(); //sql variables $server = "localhost"; $user = "root"; $pass = ""; $db = "cameo"; //iff user isn't logged in, try to log them in if(!isset($_SESSION['username'])){ if(isset($_POST['submit'])){ //connect to database $dbc = mysqli_connect($server, $user, $pass, $db); //grab entered form data $user_username = mysqli_real_escape_string($dbc, trim($_POST['username'])); $user_password = mysqli_real_escape_string($dbc, trim($_POST['username'])); //if both username and password are entered then find a match in the database if((!empty($user_username)) && (!empty($user_password))) { //look up the username and password in the database $query = "SELECT username FROM cameo WHERE username = '$user_username' AND '$user_password'"; $data = mysqli_query($dbc, $query); //authenticate if data matches a row if(mysqli_num_rows($data) == 1){ //login is okay $row = mysqli_fetch_array($data); $_SESSION['username'] = $row['username']; $_SESSION['is_admin'] = $row['is_admin']; header('Location:index.php'); } else { //username and password are incorrect or missing, so send a message $error_msg = 'Sorry, you must enter a valid username and password to log in.'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/ Share on other sites More sharing options...
BK87 Posted December 14, 2010 Share Posted December 14, 2010 add one more bracket in the end... } also consider buying phpdesigner. it only costs $37 saves you from all a lot of headache. or another ideal ide. Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147052 Share on other sites More sharing options...
Skatecrazy1 Posted December 14, 2010 Author Share Posted December 14, 2010 um added another curly bracket at the end, now i get this error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Program Files\xampp\htdocs\cameo\login.php on line 26 numrows shouldn't return a boolean should it? this is quite confusing it should just return the number of rows found not a true/false Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147173 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 $data = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); I don't think it successfully did that query... Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147176 Share on other sites More sharing options...
Adam Posted December 14, 2010 Share Posted December 14, 2010 mysqli_query() returns false (a boolean) on failure, which suggests there's a syntax error in your SQL. You should always handle the error when making query calls: $data = mysqli_query($dbc, $query) or trigger_error('SQL Error: ' . mysqli_error($dbc), E_USER_ERROR); Or to unify the handling you could define a function that does similar and call that in the 'or' clause. Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147178 Share on other sites More sharing options...
Skatecrazy1 Posted December 15, 2010 Author Share Posted December 15, 2010 ah, i referenced the database name rather than the table in the query Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147526 Share on other sites More sharing options...
Skatecrazy1 Posted December 15, 2010 Author Share Posted December 15, 2010 shooot, now i'm running this code: <?php session_start(); //sql variables $server = "localhost"; $user = "root"; $pass = ""; $db = "cameo"; //iff user isn't logged in, try to log them in if(!isset($_SESSION['username'])){ if(isset($_POST['submit'])){ //connect to database $dbc = mysqli_connect($server, $user, $pass, $db); //grab entered form data $user_username = mysqli_real_escape_string($dbc, trim($_POST['username'])); $user_password = mysqli_real_escape_string($dbc, trim($_POST['username'])); //if both username and password are entered then find a match in the database if((!empty($user_username)) && (!empty($user_password))) { //look up the username and password in the database $query = "SELECT * FROM users WHERE username = '$user_username' AND '$user_password'"; $data = mysqli_query($dbc, $query); //authenticate if data matches a row if(mysqli_num_rows($data) == 1){ //login is okay $row = mysqli_fetch_array($data); $_SESSION['username'] = $row['username']; $_SESSION['is_admin'] = $row['is_admin']; header('Location:index.php'); } else { //username and password are incorrect or missing, so send a message $error_msg = 'Sorry, you must enter a valid username and password to log in.'; } } } } ?> and the script is working but I come up with a blank page and the header redirect back to index isn't working Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147527 Share on other sites More sharing options...
Adam Posted December 15, 2010 Share Posted December 15, 2010 At a guess, I'd say you're not getting any rows back (or more than 1), and the so the condition below is evaluating to false: if(mysqli_num_rows($data) == 1){ In which case all you're doing is assigning $error_msg a value, not actually displaying anything. Try adding a var_dump just before to double check: var_dump(mysqli_num_rows($data)); Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147580 Share on other sites More sharing options...
sasa Posted December 15, 2010 Share Posted December 15, 2010 your query is wrong try something like $query = "SELECT * FROM users WHERE username = '$user_username' AND `password` = '$user_password'"; Quote Link to comment https://forums.phpfreaks.com/topic/221595-unexpected-end/#findComment-1147600 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.