T_ELSE error on line 38, help will be appreciated

  • PalmettoBuckeye
  • Born
  • Born
  • PalmettoBuckeye
  • Posts: 1

Post 3+ Months Ago

I am new to php and building a login page. I keep geeting an unexpected T_ELSE error. I have gone through the code line by line and cannot find what is causing this. Any help will be appreciated, greatly.
Thanks (the code is below)

Code: [ Select ]
<?php require_once("includes/constants.php");?>
<?php include_once("includes/connection.php");?>
<?php
    include_once("includes/functions.php");
    //start form processing
    if (isset($_POST['submit'])) { //Form has been submitted.
    $errors = array();
    
    //perform validations on the form data
    $required_fields = array('username','password');
    $errors = array_merge($errors, check_required_fields($required_fields,$_POST));
    $fields_with_lengths = array('username' => 30,'password'=>30,);
    $errors = array_merge($errors,check_max_field_lengths($fields_with_lengths,$_POST));
    
    $username = trim(mysql_prep($_POST['username']));
    $password = trim(mysql_prep($_POST['password']));
    $hashed_password = sha1($password);
    
    if(empty($errors)){
        $query = "INSERT INTO users (
            username, hashed_password
            ) VALUES (
                '{$username}','{$hashed_password}'
            )";
        $result = mysql_query($query, $connection);
        if ($result) {
            $message = "The user was successfully created.";
        } else {
            $message = "The user could not be created.";
            $message .= "<br />" . mysqlerror();
            }
    } else {
        if (count($errors) == 1) {
            $message = "There was 1 error in the form.";
        } else {
            $message = "There were" .count($errors). "errors in the form.";
            }
    } else { //Form has not been submitted
    $username ="";
    $password ="";
}
?>
<html>
    <body>
        <h2>Please Login</h2>
        <?php if (!empty($errors)) { echo "<p=class=\"message\">"$message"<p>" ;}?>
        <?php if (!empty($errors)) { display_errors($errors);}?>
        
<form id="Login" action="login.php" method="post" name="loginform">
<table width="180" border="1" cellspacing="2" cellpadding="0">
    <tr>
        <td><label>Username</label><input type="text" name="username" maxlength="24" value-"<?php echo htmlentities($username);?>"</td>
    </tr>
    <tr>
        <td><label>Password</label><input type="password" name="password" maxlength="30" value-"<?php echo htmlentities($password);?>"</td>
    </tr>
    <tr>
    <td><button name="Submit" type="Submit">Login</button></td>
    </tr>
</table>
</form>
    </body>
</html>
<?php mysql_close($connection);?>
  1. <?php require_once("includes/constants.php");?>
  2. <?php include_once("includes/connection.php");?>
  3. <?php
  4.     include_once("includes/functions.php");
  5.     //start form processing
  6.     if (isset($_POST['submit'])) { //Form has been submitted.
  7.     $errors = array();
  8.     
  9.     //perform validations on the form data
  10.     $required_fields = array('username','password');
  11.     $errors = array_merge($errors, check_required_fields($required_fields,$_POST));
  12.     $fields_with_lengths = array('username' => 30,'password'=>30,);
  13.     $errors = array_merge($errors,check_max_field_lengths($fields_with_lengths,$_POST));
  14.     
  15.     $username = trim(mysql_prep($_POST['username']));
  16.     $password = trim(mysql_prep($_POST['password']));
  17.     $hashed_password = sha1($password);
  18.     
  19.     if(empty($errors)){
  20.         $query = "INSERT INTO users (
  21.             username, hashed_password
  22.             ) VALUES (
  23.                 '{$username}','{$hashed_password}'
  24.             )";
  25.         $result = mysql_query($query, $connection);
  26.         if ($result) {
  27.             $message = "The user was successfully created.";
  28.         } else {
  29.             $message = "The user could not be created.";
  30.             $message .= "<br />" . mysqlerror();
  31.             }
  32.     } else {
  33.         if (count($errors) == 1) {
  34.             $message = "There was 1 error in the form.";
  35.         } else {
  36.             $message = "There were" .count($errors). "errors in the form.";
  37.             }
  38.     } else { //Form has not been submitted
  39.     $username ="";
  40.     $password ="";
  41. }
  42. ?>
  43. <html>
  44.     <body>
  45.         <h2>Please Login</h2>
  46.         <?php if (!empty($errors)) { echo "<p=class=\"message\">"$message"<p>" ;}?>
  47.         <?php if (!empty($errors)) { display_errors($errors);}?>
  48.         
  49. <form id="Login" action="login.php" method="post" name="loginform">
  50. <table width="180" border="1" cellspacing="2" cellpadding="0">
  51.     <tr>
  52.         <td><label>Username</label><input type="text" name="username" maxlength="24" value-"<?php echo htmlentities($username);?>"</td>
  53.     </tr>
  54.     <tr>
  55.         <td><label>Password</label><input type="password" name="password" maxlength="30" value-"<?php echo htmlentities($password);?>"</td>
  56.     </tr>
  57.     <tr>
  58.     <td><button name="Submit" type="Submit">Login</button></td>
  59.     </tr>
  60. </table>
  61. </form>
  62.     </body>
  63. </html>
  64. <?php mysql_close($connection);?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Notice how in this block you have two ELSE statements ?
Code: [ Select ]
 } else {
        //...
    } else { //Form has not been submitted
  1.  } else {
  2.         //...
  3.     } else { //Form has not been submitted


You can only have one ELSE statement in an IF/ELSE construct. You can however, have multiple ELSEIF statements.

You'll need to re-think that block of code so that it will use a single IF, possibly multiple ELSEIF statements, and possibly a single ELSE.

IF: One
ELSEIF: Multiple
ELSE: One

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 71 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.