PHP注册/登录错误

标签 php mysql registration

<分区>

我正在尝试创建注册/登录系统。但是,我遇到了一些问题。我无法理解我的代码中的错误在哪里。 这是我的 server.php 和 register.php。浏览器显示错误在第 65 行。“解析错误:语法错误,意外的‘;’”。在我看来 ;必须在那里。

    <?php
session_start();
    $username = "";
    $email = "";
    $errors = array();
// Connect to the database
$db = mysqli_connect('localhost', 'root', '', 'lead2pro');
// If the register button is clicked
    if(isset($_POST['register'])) {
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $email = mysqli_real_escape_string($db, $_POST['email']);
        $password_1 = mysqli_real_escape_string($db ,$_POST['password_1']);
        $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
        // Ensure that form fields are filled properly
    if(empty($username)) {
            array_push($errors, "Username is required!");
        }
    if(empty($email)) {
            array_push($errors, "Email is required!");
        }
    if(empty($password_1)) {
            array_push($errors, "Password is required!");
        }
    if($password_1 != $password_2) {
        array_push($errors, "The two passwords do not match");
        }
        // If there are no errors, save user to database
        if(count($errors) == 0) {
            $password = md5($password_1); // Hashin the password before storing in database
            $sql = "INSERT INTO users (username, email, password) VALUES('$username', '$email', '$password')";
            mysqli_query($db, $sql);
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: ../system.php'); // Redirect to game location
        }
    }
// log user in from login page 
    if(isset($_POST['login'])) {
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $password = mysqli_real_escape_string($db, $_POST['password']);
        // Ensure that form fields are filled properly
    if(empty($username)) {
            array_push($errors, "Username is required!");
        }
    if(empty(password)) {
            array_push($errors, "Password is required!");
        }
        if(count($errors) == 0){
            $password = md5($password); // Encrypt password before comparing this one with the one in database
            $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
            $result = mysqli_query($db, $query);
            $if (mysqli_num_rows($result) == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: ../system.php'); // Redirect to main page location
            } else {
                array_push($errors, "Wrong username/password combination");
                    header('location: ../php/login.php');
            }
        }
    }
//logout
    if(isset($_GET['logout'])) {
        session_destroy();
        unset($_SESSION['username']);
        header('location: ../php/login.php');
    }
?>

这是我的register.php

<?php include('../includes/server.php');?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Manager | Register</title>
    <link rel="stylesheet" href="../css/reg.css">
</head>
<body>
    <div class="header">
        <h2>Register</h2>
    </div>
<!--    Display validation errors here!     -->
   <?php include('../includes/errors.php');   ?>
    <form action="register.php" method="post">
        <div class="input-group">
            <label>Username</label>
            <input type="text" name="username" value="<?php echo $username; ?>">
        </div>
        <div class="input-group">
            <label>Email</label>
            <input type="text" name="email" value="<?php echo $email; ?>">
        </div>
        <div class="input-group">
            <label>Password</label>
            <input type="password" name="password_1">
        </div>
        <div class="input-group">
            <label>Confirm Password</label>
            <input type="password" name="password_2">
        </div>
        <div class="input-group">
        <button type="submit" name="register" class="btn">Register</button>
        </div>
        <p>
            Already a member? <a href="login.php">Sign in</a>
        </p>
    </form>
</body>
</html>

最佳答案

问题出在不同的线路上:

$if (mysqli_num_rows($result) == 1) {
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: ../system.php'); // Redirect to main page location
}

$ 不应该出现在 if 的前面。

关于PHP注册/登录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212534/

相关文章:

php - 方法 [validateRequire] 不存在

php - 仅显示重复列值中的第一个

php - 未定义的属性:Illuminate\Database\MySqlConnection::$name

backup - 共享软件执行与时间机器

PHP 从 MySql 拉取

mySQL PDO 未在 phpinfo() 中启用

用于选择最后插入的行的 php 代码

php - 无法弄清楚如何只在 mysql 中查询最近 30 天的结果

com - 注册免费 Com 和 dll list

php - 自动注册后用户身份验证