【问题标题】:PHP file doesn't execute code after connecting to a databasePHP文件在连接到数据库后不执行代码
【发布时间】:2018-07-19 02:03:51
【问题描述】:

运行我的 PHP 代码后,屏幕上会打印 hello1,但不会打印 hello2。我认为我的连接代码有问题。

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

顺便说一句,我在 MacBook Air 上运行 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();
?>

【问题讨论】:

  • 一个;你没有连接到你的数据库。我将从使用调试工具开始,例如 php 的错误报告和对数据库和查询的错误检查。然后,您可以编辑您的帖子以包含错误;你确实有很多,我宁愿“你”修复你的代码,这样你就知道下次该做什么和不做什么。
  • 谢谢,我会努力寻找自己的,但你现在想指出至少一个错误
  • 当然,这是自上而下的第一个:您将$connect 声明为连接变量,然后您切换到$mysqli。变化的答案在前者。然后,您可以通过错误报告php.net/manual/en/function.error-reporting.php 和查询php.net/manual/en/mysqli.error.php 的错误检查来继续工作
  • 我不明白你的意思,我只是存储函数在创建与数据库的连接后返回的对象以便以后使用它,请确保你的回答现在更有意义
  • 昨天晚上我打算发布错误报告,但为时已晚,今天我忘记了,哈哈,谢谢你福特提醒我

标签: php mysql session authentication mamp


【解决方案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');

【讨论】:

  • 嗨谢谢你的回复,我使用的是php 7.2所以Mysqli函数在这个版本中不存在只有PDO或Mysqli_connect,我将服务器保存为对象的原因是为了使用它后来,我也看到了以面向对象的方式使用该函数的教程,它仍然有效,但是非常感谢您的建议,我会尝试一下,您是否碰巧看到任何其他错误?
  • 我不确定你从哪里得到我建议你使用 mysql_ 的想法。我的例子是使用 mysqli。我只是指出了你在混合 mysqli 过程代码和面向对象代码时所犯的错误。
  • 哦,对不起,没注意到你,我后来搬到了今天晚上
  • 不用担心。假设这有助于您解决问题,您可以接受我的回答。
  • 我的问题是数据库而不是代码代码很好,我没有密码是一个 MYSQL 术语,所以当我将它设为列名时,它给了我问题
【解决方案2】:

您也可以使用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();
}

附: - 在$mysqli-&gt;set_charset("utf-8"); $mysqli 中没有定义,在这里使用$connect

【讨论】:

  • 谢谢你,这确实帮助我修复了后面的一些部分,但是因为我急于提交,所以忘了说谢谢
  • 没问题。很高兴它有帮助
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多