php文件在连接到数据库后不执行代码

5ssjco0h  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(429)

运行php代码后,屏幕上会显示hello1,但不会显示hello2。我想我的连接代码有问题。
我找不到我的代码有什么问题。不幸的是,我的代码似乎正确,即使经过多次检查。我该怎么修?
顺便说一句,我正在MacBookAir上运行mamp。

<?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();
?>
gtlvzcf8

gtlvzcf81#

在脚本开始时:

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');
nlejzf6q

nlejzf6q2#

你也可以使用 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 在这里

相关问题