PHP help with If and Else code

I have been messing around with this code for sometime. Each time I think I have it correct, it displays the same error.

Error:
PHP Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\login.php on line 57



<?php
/*
-----------------------December 3, 2010-----------------------
Page Name: login.php
Date   /  Modifications
=======/=======================================================

===============================================================
*/

// User has 3 trys to enter correct username and password
// increment by one
$count = ($count + 1);

if ($_POST['username'])
{
    //Connect to the database through our include
    include_once "connect_to_mysql.php";

    // Protect against trash
    $username = stripslashes($_POST['username']);
    $username = strip_tags($username);
    $username = mysql_real_escape_string($username);

    $password = ereg_replace("[^A-Za-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
    $password = md5($password);
}

// Make query and then register database data and display user info
// cannot be changed by member into SESSION variables.
// Data that you want member to be able to change -
// should never be set into a SESSION variable.
$sql = mysql_query("SELECT * FROM register WHERE username='$username'
       AND password='$password' limit=1");
$login_check = mysql_num_rows($sql);

if($login_check > 0)
{
    while($row = mysql_fetch_array($sql))
    {
        // Get member ID into a session variable
        $id = $row["id"];
        session_register('id');
        $_SESSION['id'] = $id;
        // Get member username into a session variable
	    $username = $row["username"];
        session_register('username');
        $_SESSION['username'] = $username;
        // Update login field for this member now
        mysql_query("UPDATE user_log SET login=now() WHERE id='$id'");
        // Print success message here if all went well then exit the script
        echo "Successfully connected to Alumni database";
		header("location: memPage.shtml php?id=$id");
		exit();
     }
// close log-in check
Error ----> else
{
    // Print login failure message to the user and link them back to your login      page
    print '<br><br>
    <font color="#FF0000">
    No match for your entered username and password were found in our  records. Please, try again.
    </font>
    <br><br>
    <a href="login.php">Click here</a> to try loging in again.';
    exit();
}

?>


Could someone please show me where I went wrong? Please just don’t say, you are missing a curly b5acket. Show me where and why.

check you {}'s

you have a mis-match in your code. a bracket is missing.

and yes, I removed your “Error ---->” note before checking

Thanks for the fast reply. I’ll check again, I haven’t found it yet, I’ll try again. What I’m figuring and tell me if i’m wrong.

!. There must be an even number of brackets.
2. Since the else state ment is in error, it should be an error with the first if statement at the top of the code.

I found it!!! Thanks for your help.
BOB…

Try this:



// remove echo '<br />',  after debugging and following two lines 
error_reporting(-1);
ini_set('display_errors', TRUE);

// YOUR MAIN PROBLEM
// remove = in limit

// actually the username and password should be unique and return either 0 or 1 without using LIMIT
echo '<br />', 
$sql = "
         SELECT * 
         FROM     register 
         WHERE username='$username' 
         AND 
         password='$password' 
         LIMIT 0,1
         "; 

echo '<br />',
$resource = mysql_query($sql); // 

echo '<br />', 
$login_check = mysql_num_rows($resource); 

echo '<br />', 
die('ONCE ERRORS REMOVED, MOVE TO NEXT PROBLEM');


in total yes.

there must be as many { as there are }. The same applies to ()'s.

most of the the time yes, and it’s certainly a good place to start looking.

glad you sorted it out in the end :slight_smile:

John;
Thanks for the approach. I will use it to debug my other pages.:wink:

This is emphatically not the correct forum for this post. Application design is for discussing issues related to, well, application design. Diagnosing a basic parse error is a question far more suited to the main PHP forum and does not belong in this one, as this forum is for “topics such as Object Orientated Programming, Design Patterns and other software development techniques related to PHP

In most forums on the internet it is considered somewhat rude to make an account and post at random in forums without stopping to read what their general purpose is. That information is in this stickied thread. Please review it before making additional threads. and welcome to sitepoint.

Thank you Michael. Thread moved to the PHP forum.