Jump to content

unexpected $end


Skatecrazy1

Recommended Posts

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.';
  }
}
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.