Jump to content

ElseIf (nesting/syntax) Script Error. A new set of eyes please.


Recommended Posts

I've been hacking at this for 3 hours now.. My syntax seems to be screwed up on something, but I cannot find it for the life of me!


Error thrown is obvious I am missing or have something nested wrong!

Parse error: parse error in C:\wamp\www\php\php_study\login_processor.php on line 22



Here is my code. Error is being thrown on the line of the elseif statment.


  $cxn = mysqli_connect($host, $user, $pass, $db) or die("Query died: connect");
  $sql = "SELECT username FROM users WHERE username = '$_POST[username]'";
  $result = mysqli_query($cxn, $sql) or die("Query Died: username");
  $num = mysqli_num_rows($result);
  if ($num > 0) {
      $sql = "SELECT username FROM users WHERE username='$_POST[username]' AND password=md5('$_POST[password]')";
      $result2 = mysqli_query($cxn, $sql) or die("Query died: password");
      $num2 = mysqli_num_rows($result2);
      if ($num2 > 0) {
          $_SESSION['logname'] = $_SESSION['username'];
      } //if ($num2 > 0)
      header("Location: the_secret_page.php");
  } //if ($num > 0)
  else {
      $message1 = "The login name, '$_POST[username]' does not exist in the database. Please try again";
      $username = strip_tags(trim($_POST[username]));
  } //else
  elseif ($num == 0) {
      $message1 = "The username you entered does not exist. Please try again.s";
  } //elseif ($num == 0)


I don't think my form html/php code is necessary, but I'll include it for reference:


<h1>Login Form</h1>

  $fields_1 = array("username" => "User Name", "password" => "Password");

<form method="post" action="login_processor.php">
<legend>Login Form</legend>
  if (isset($message_1)) {
      echo "$message_1";
  } //if (isset($message_1))
  foreach ($fields_1 as $field => $value) {
      if (preg_match("/pass/i", $field))
          $type = "password";
          $type = "text";
      echo "<div id='field'>
<label for='$field'>$value</label>
<input id='$field' name='$field' type='$type' value='" . @$$field . "' size='20' maxlength='50' />
  } //foreach ($fields_1 as $field => $value)
<input type="submit" name="Button" value="Login" />





Link to comment
Share on other sites

I think I just figured it out. My elseif needed to go before the else.. The error is no longer being throw, so my guess is I got it..


My code is far from done (cleaned up and secured), but I'm open for any suggestions or critique on what I have so far..

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

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

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.