ttnaddy16 Posted May 11, 2019 Share Posted May 11, 2019 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(); } Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/ Share on other sites More sharing options...
benanamen Posted May 11, 2019 Share Posted May 11, 2019 (edited) The proper syntax is not else, else, else....https://www.php.net/manual/en/control-structures.elseif.php Edited May 11, 2019 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/#findComment-1566592 Share on other sites More sharing options...
ttnaddy16 Posted May 11, 2019 Author Share Posted May 11, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/#findComment-1566593 Share on other sites More sharing options...
ttnaddy16 Posted May 11, 2019 Author Share Posted May 11, 2019 Figured it out used one parenthesis instead of a curly bracket for my else block. Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/#findComment-1566596 Share on other sites More sharing options...
gizmola Posted May 11, 2019 Share Posted May 11, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/#findComment-1566598 Share on other sites More sharing options...
mac_gyver Posted May 11, 2019 Share Posted May 11, 2019 (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 May 11, 2019 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/#findComment-1566601 Share on other sites More sharing options...
ginerjm Posted May 11, 2019 Share Posted May 11, 2019 (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 May 11, 2019 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/#findComment-1566603 Share on other sites More sharing options...
ginerjm Posted May 11, 2019 Share Posted May 11, 2019 BTW - the extra semi is in your query statement. Quote Link to comment https://forums.phpfreaks.com/topic/308691-parse-error-syntax-error-unexpected-line-44/#findComment-1566604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.