Jump to content
ttnaddy16

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

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

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Posted (edited)
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

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
Share on other sites

BTW - the extra semi is in your query statement.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.