连接到数据库后,PHP文件不执行代码

时间:2018-07-19 02:03:52

标签: php mysql session authentication mamp

运行我的PHP代码后,hello1将显示在屏幕上,而不是hello2。我认为我的连接代码有问题。

我找不到我的代码有什么问题。对我来说不幸的是,即使经过多次检查,我的代码也似乎是正确的。我该如何解决?

顺便说一句,我正在MAMP上运行MacBook Air

<?php
    echo "hello1";
    $connect = mysqli_connect("localhost:8888", "Capstone", "", "capstone");
    $mysqli->set_charset('utf8');

    echo "hello2";

    if (!$connect) {
        printf("Connection failed: %s\n", $mysqli->connect_error);
        die();
        echo "hello3";
    }
    session_start();

    if (isset($_POST["Sign Up"]))
    {
        if (empty($_POST["Email"]) || empty($_POST["Password"]))
        {
            echo '<script> alert ("Both Feldsa are required)</script">';
        }
        else
        {
            $_SESSION['email'] = $_POST['Email'];
            $_SESSION['password'] = $_POST['Password'];
            $_SESSION['Repeatpassword'] = $_POST['Repeatpassword'];
            $_SESSION['name'] = $_POST['name'];
            $_SESSION['weight'] = $_POST['weight'];
            $_SESSION['feet'] = $_POST['feet'];
            $_SESSION['inches'] = $_POST['inches'];
            $_SESSION['age'] = $_POST['age'];
            $_SESSION['goal'] = $_POST['Goal'];

            // Escape all $_POST variables to protect against SQL injection
            $email = $mysqli->escape_string($_POST['email']);
            $password = $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
            $RepPassword = $mysqli->escape_string(password_hash($_POST['Repeatpassword'], PASSWORD_BCRYPT));

            $name = $mysqli->escape_string($_POST['name']);
            $Weight = $mysqli->escape_string($_POST['weight']);
            $feet = $mysqli->escape_string($_POST['feet']);
            $inches = $mysqli->escape_string($_POST['inches']);
            $age = $mysqli->escape_string($_POST['age']);
            $goal = $mysqli->escape_string($_POST['goal']);
            $hash = $mysqli->escape_string(md5(rand(0, 1000)));

            // Check if user with that email already exists

            // We know user email exists if the rows returned are more than 0
            $result = $mysqli->query("SELECT * FROM User WHERE Email_Address='$email'") or die($mysqli->error);
            if ($result->num_rows > 0) {

                $_SESSION['message'] = 'User with this email already exists!';
            }
            else { // Email doesn't already exist in a database, proceed...

                // active is 0 by DEFAULT (no need to include it here)
                $sql = "INSERT INTO User (Email_Address, Password, Full Name, Weight, Feet, Inches, Age, Goal, hash) "
                        . "VALUES ('$email', 'password', 'name', 'Weight', 'feet', 'inches', 'age', 'goal', 'hash')";
            }
            if (! $mysqli->query($sql)
            {
                $_SESSION['message'] = 'Registration successfully';
                echo $_SESSION['message'];

                header("location: loginaccount.html");
            }
        }
        else {
            $_SESSION['message'] = 'Registration failed!';
            echo $_SESSION['message'];
        }
    }

    if (isset($_POST["Login"]))
    {
        $email = $mysqli->escape_string($_POST['Email']);
        $result = $mysqli->query("SELECT * FROM User WHERE Email_Address='$email'");
        if ($result->num_rows == 0) { //
            {
                $_SESSION['message'] = "User with that email doesn't exist!";
                echo $_SESSION['message'];
            }
            else {
                $user = $result->fetch_assoc();
                if (password_verify($_POST['password'], $user['Password'])) {
                    $_SESSION['email'] = $user['Email_Address'];
                    $_SESSION['name'] = $user['Full Name'];
                    $_SESSION['weight'] = $user['Weight '];

                    $_SESSION['feet'] = $user['Feet '];
                    $_SESSION['inches'] = $user['Inches '];
                    $_SESSION['age'] = $user['Age '];
                    $_SESSION['goal'] = $user['Goal '];
                    $_SESSION['logged_in'] = true;
                    $_SESSION['active'] = $user['Active'];
                    header("location: loginaccount.html");
                }
            }
            mysqli_close($connect);
            session_destroy();
?>

2 个答案:

答案 0 :(得分:1)

在脚本开头:

echo "hello1";
$connect = mysqli_connect("localhost:8888", "Capstone", "", "capstone");
$mysqli->set_charset('utf8');

在这里的第3行,您尝试使用$ mysqli。该变量不存在。您尚未声明它,因此,当您尝试引用一个对象的方法时,实际上将是一个不存在的变量,此时您将得到一个PHP运行时错误。

实际上比这更糟,因为您将过程mysqli与面向对象的mysqli混合在一起。您真正需要的是这个,但显而易见的问题是您的mysqli连接变量名为$connect

echo "hello1";
$connect = new mysqli("localhost:8888", "Capstone", "", "capstone");
$connect->set_charset('utf8');

答案 1 :(得分:0)

您还可以使用try/catch来查找有关错误的更多信息

try{
    echo "hello1";
    $connect = mysqli_connect("localhost:8888", "Capstone", "", "capstone");
    $mysqli->set_charset('utf8');
    echo "hello2";

}
catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}

P.S。 -在$mysqli->set_charset("utf-8"); $mysqli未定义的地方,请在此处使用$connect