【问题标题】:How to solve 'Parse error syntax error unexpected end of file' in PHP file? [duplicate]如何解决 PHP 文件中的“Parse error syntax error unexpected end of file”? [复制]
【发布时间】:2021-02-24 08:38:34
【问题描述】:

我查看了我的代码,但我不知道为什么这个错误不断出现。

我得到的确切错误信息 '解析错误:语法错误,第 92 行 C:\wamp\www\includes\functions.inc.php 中的文件意外结束'

这是我的代码:

<?php
#since this file contains solely PHP there is no need for the closing tag
#by convention it is better to check if something fails first then succeeds
function emptyInputSignup($username, $password, $passwordRepeat){
    $result = Null;
    if (empty($username) || empty($password) || empty($passwordRepeat)){
        $result = true;
    }
    else{
        $result = false;
    }
    return result;
}

function invalidUsername($username){
    $result = Null;
    #if there is a mistake
    if (!preg_match("/^[a-zA-Z0-9]*$/", $username)){
        $result = true;
    }
    else{
        $result = false;
    }
    return result;
}
function passwordMatch($password, $passwordRepeat){
    $result = Null;
    #if there has been a mistake
    if ($password !== $passwordRepeat){
        $result = true;
    }
    else{
        $result = false;
    }
    return result;
}

function usernameExists($conn, $username){
    $sql = "SELECT * FROM users WHERE username = ?";
    #we have created a prepared statement to prevent SQL injections
    $stmt = mysqli_stmt_init($conn);
    $result = Null;
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=stmtfailed");
        exit();
    }
    
    mysqli_stmt_bind_param($stmt, "s", $username);
    mysqli_stmt_execute($stmt);
    
    $resultData = mysqli_stmt_get_result();
    
    if($row = mysqli_fetch_assoc($resultData)){ 
        return $row;        
        
    }
    else{
        $result = false;
        return $result;
}

function passwordValid($password){
    $result = Null;
    #if there is a mistake
    if (strlen($password) <= 5 && !preg_match("~[0-9]+~", $password)){
        $result = true;
    }
    else{
        $result = false;
    }
    return result;
}

这是我认为错误所在的函数。

function createUser($conn, $username, $password){
    $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
    #we have created a prepared statement to prevent SQL injections
    $stmt = mysqli_stmt_init($conn);
    #this checks if the statement is feasible
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=stmtfailed");
        exit();
    }
    #returns hash of a password
    #this inbuilt function is constantly updated
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);   
    mysqli_stmt_bind_param($stmt, "ss", $username, $hashedPassword);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    header("location: ../signup.php?error=none");
    exit;   
} #this is line 92 but I do not see where the issue is here

我已经对缺少括号和/或 php.ini 的状态如何导致此错误进行了一些研究,但是 short_open_tag 已设置为打开。 是什么导致了错误?

【问题讨论】:

  • 算括号。另外,我看到还有一个错字,错误消息/警告如undefined constant
  • 试试usernameExists
  • ^ usernameExists 函数中最后的 else 语句。

标签: php error-handling


【解决方案1】:

function usernameExists 关闭其他

来自:

function usernameExists($conn, $username){
    $sql = "SELECT * FROM users WHERE username = ?";
    #we have created a prepared statement to prevent SQL injections
    $stmt = mysqli_stmt_init($conn);
    $result = Null;
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=stmtfailed");
        exit();
    }
    
    mysqli_stmt_bind_param($stmt, "s", $username);
    mysqli_stmt_execute($stmt);
    
    $resultData = mysqli_stmt_get_result();
    
    if($row = mysqli_fetch_assoc($resultData)){ 
        return $row;        
        
    }
    else{
        $result = false;
        return $result;
}

改成

function usernameExists($conn, $username){
    $sql = "SELECT * FROM users WHERE username = ?";
    #we have created a prepared statement to prevent SQL injections
    $stmt = mysqli_stmt_init($conn);
    $result = Null;
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=stmtfailed");
        exit();
    }
    
    mysqli_stmt_bind_param($stmt, "s", $username);
    mysqli_stmt_execute($stmt);
    
    $resultData = mysqli_stmt_get_result();
    
    if($row = mysqli_fetch_assoc($resultData)){ 
        return $row;        
        
    }
    else{
        $result = false;
        return $result;
      }//<---this line
}

【讨论】:

  • 请不要发布仅指出印刷问题或缺少字符的答案。这样的答案不太可能帮助未来的访问者,因为它们特定于 OP 的代码。相反,请根据help center 标记或投票以将问题关闭为题外话。
猜你喜欢
  • 2016-06-17
  • 2016-10-21
  • 2018-09-28
  • 2012-03-13
  • 2014-10-06
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 2011-01-10
相关资源
最近更新 更多