Jump to content

Parse error: syntax error, unexpected ';' line 44


ttnaddy16

Recommended Posts

I have an error on this php page and am getting confused with the curly brackets. I hope a set of fresh eyes will help me find the bracket I'm missing or misplaced. Any help would be greatly appreciated. Here's my code

<?php
if (isset($_POST['login-submit'])) {
	
require 'dbh.inc.php';

$mailuid = $_POST['mailuid'];
$password = $_POST['pwd'];

if (empty($mailuid) || empty($password)) {
header("Location: ../index.php?error=emptyfields");
exit();
}
else {
$sql = "SELECT * FROM users WHERE uidUsers=? OR emailUsers=?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=sqlerror");
exit();	
}
else {
mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
$pwdCheck = password_verify($password, $row['pwdUsers']);
if ($pwdCheck == false) {
header("Location: ../index.php?error=wrongpwd");
exit();
}
else if ($pwdCheck == true) {
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userUid'] = $row['uidUsers'];

header("Location: ../index.php?login=success");
exit();
}
else {
header("Location: ../index.php?error=wrongpwd");
exit();	
}
}
else (
header("Location: ../index.php?error=nouser");
exit();	
)
}
}
}
else {
header("Location: ../index.php");
exit();	
}

 

Link to comment
Share on other sites

Do I use a switch statement for the last 3 consecutive else statements, I'm getting confused because this is exactly what the tutorial video I watched shows as the solution without a switch statement. I'm very new to this if you can show me the structure of those else statements and how they should be that would be super helpful.

Link to comment
Share on other sites

1 hour ago, ttnaddy16 said:

Figured it out used one parenthesis instead of a curly bracket for my else block. 

Any decent editor with PHP support should have shown you the issue with a missing bracket or parens.  What are you using to edit your code?

It also would have been easy to see if you indented your code.  I'm not sure if that was an issue with cutting and pasting or if you are writing your code that way, but if you are, that is a terrible habit and one you should remedy immediately.  Again most editors support basic indent/outdent pretty well in most cases.

One thing that would help is to have your 

} else { 

and

} else if () {

On one line.

Here's what I'd expect your code to look like in an editor:

 

<?php
if (isset($_POST['login-submit'])) {

require 'dbh.inc.php';

$mailuid = $_POST['mailuid'];
$password = $_POST['pwd'];

if (empty($mailuid) || empty($password)) {
    header("Location: ../index.php?error=emptyfields");
    exit();
} else {
    $sql = "SELECT * FROM users WHERE uidUsers=? OR emailUsers=?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../index.php?error=sqlerror");
        exit();
    } else {
        mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        if ($row = mysqli_fetch_assoc($result)) {
            $pwdCheck = password_verify($password, $row['pwdUsers']);
            if ($pwdCheck == false) {
                header("Location: ../index.php?error=wrongpwd");
                exit();
            } else if ($pwdCheck == true) {
                session_start();
                $_SESSION['userId'] = $row['idUsers'];
                $_SESSION['userUid'] = $row['uidUsers'];

                header("Location: ../index.php?login=success");
                exit();
            } else {
                header("Location: ../index.php?error=wrongpwd");
                exit();
            }
        } else (
            header("Location: ../index.php?error=nouser");
            exit();
        }
    } else {
        header("Location: ../index.php");
        exit();
    }
}

 

With proper indentation, the structure of the logic is visible and can be followed.

Link to comment
Share on other sites

10 hours ago, ttnaddy16 said:

am getting confused with the curly brackets.

you also have a "cannot see the forest for the trees" problem with a lot of unnecessary variables, statements, and conditional tests (boolean-like values can only be true or false, so comparing a value first with a true and then later a false is redundant clutter.)

by putting the form and the form processing code on separate pages, you are doubling the amount of logic you need.

also, if you switch to the much simpler php PDO extension and use exceptions to handle database statement errors (and in most cases let php catch and handle any database errors) a lot of the database related code will go away.

Edited by mac_gyver
Link to comment
Share on other sites

The way I make long logic blocks more readable is to use indentation.  Try reading thru this and seeing how much simpler it is to follow with the visual improvements:

	if (isset($_POST['login-submit'])) 
{
    require 'dbh.inc.php';
    $mailuid = $_POST['mailuid'];
    $password = $_POST['pwd'];
    if (empty($mailuid) || empty($password)) 
    {
        header("Location: ../index.php?error=emptyfields");
        exit();
    }
    else 
    {
        $sql = "SELECT * FROM users WHERE uidUsers=? OR emailUsers=?;";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) 
        {
            header("Location: ../index.php?error=sqlerror");
            exit();    
        }
        else 
        {
            mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            if ($row = mysqli_fetch_assoc($result)) 
            {
                $pwdCheck = password_verify($password, $row['pwdUsers']);
                if ($pwdCheck == false) 
                {
                    header("Location: ../index.php?error=wrongpwd");
                    exit();
                }
                else 
                if ($pwdCheck == true) 
                {
                    session_start();
                    $_SESSION['userId'] = $row['idUsers'];
                    $_SESSION['userUid'] = $row['uidUsers'];
                    header("Location: ../index.php?login=success");
                    exit();
                }
                else 
                {
                    header("Location: ../index.php?error=wrongpwd");
                    exit();    
                }
            }
            else 
            (
                header("Location: ../index.php?error=nouser");
                exit();    
            )
        }
    }
}
else 
{
    header("Location: ../index.php");
    exit();    
}
	

(The indentation of the forum's software may not allow my structure to come thru that well).  Some people like to place the curly braces on line with the else but I like to place braces on lines by themselves to make the block structure much more apparent.

And as Gizmola pointed out - using a proper IDE that supports PHP would help to identify the pairs of braces so you can quickly see when they are out of balance.  That and mis-spelled function names and missing quotes, etc.

 

Edited by ginerjm
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.