php - 检查了查询、数据库、表名、列名等,还是不明白为什么会出现这个错误

标签 php mysql mysqli

我发生的错误:

Fatal error: Call to a member function bind_param() on boolean in C:\wamp64\www\APU\SDP\reg-list-function.php on line 82

我正在编写一个 php 脚本,管理员可以在其中批准用户的注册。我已经检查了数据库的格式、列名甚至查询,但仍然不知道为什么会出现此错误。任何帮助或建议将不胜感激!

<?php
// we will only start the session with session_start() IF the session isn"t started yet //
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
?>

<?php
// including the conn.php to establish connection with database //
  include "conn.php";
?>

<?php
// Begin of the function: Registration List's Verification Form: Registration //
// First we check the form has submitted or not //
if (isset($_POST['submit-list-reg'])) {
  // If it is, then we will retreive data from the input forms //
  $regid = $_POST["regid"];
  $reg_acccode = mysqli_real_escape_string($con, $_POST['reg-acccode']);
  $reg_pw = mysqli_real_escape_string($con, $_POST['reg-pw']);
  // Taking the current time //
  date_default_timezone_set("Etc/GMT-8");
  $now = date("Y-m-d H:i:s");
  // Variable to store Error Message //
  $error = '';
  // Alphanumeric Generator //
  function random_strings($length_of_string) {
    // String of all alphanumeric character
    $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

    // Shufle the $str_result and returns substring
    // of specified length
    return substr(str_shuffle($str_result), 0, $length_of_string);
  }

  // Sorting out the query related to the function //
  // Verify the user is an admin or not //
  $VERFYADMIN = "SELECT * FROM user
                 WHERE status = 2 AND active = 1 AND account_code = '".md5($reg_acccode)."' AND password = '".md5($reg_pw)."'";
  $VERFYADMINQ = mysqli_query($con, $VERFYADMIN);

  //***BEGIN OF PROCESS***//
  if (mysqli_num_rows($VERFYADMINQ) < 1) {
    // if the admin is not verified, then inform the user and send him back to admin panel //
    echo "<script>alert('ALERT: Information unable to be verified. Please try again.');";
    echo "window.location.href='admin_panel.html';</script>";
    exit(0);
  } else {
    // begin the process of registration //
    while (list($key,$val) = @each ($regid)) {
      // Now to verify the user's legitimacy //
      // Take the user's vercode into variable first //
      $USERVERCODE = "SELECT * FROM registration_list
                      WHERE registration_id = $val AND verified = 0";
      $USERVERCODEQ = mysqli_query($con, $USERVERCODE);
      if (mysqli_num_rows($USERVERCODEQ) < 1) {
        // if we are unable to retrieve the data of the registering user then something must gone wrong //
        echo "<script>alert('WARNING: Unable to retrieve the data. Please try again.');";
        echo "</script>";
      } else {
        while ($row = mysqli_fetch_array($USERVERCODEQ)) {
          $vercode = $row["verification_code"];
        }
          // since we got the value of the vercode then we start to define the query //
          $VERCODE = "SELECT * FROM verification_code WHERE verification_code = $vercode AND code_active = 1";
          $VERCODEQ = mysqli_query($con, $VERCODE);
          if (mysqli_num_rows($VERCODEQ) < 1) {
            // if we are unable to retrieve the data of the registering user then something must gone wrong //
            echo "<script>alert('WARNING: Unable to retrieve the info of VERCODE. Please try again.');";
            echo "</script>";
          } else {
            while ($row = mysqli_fetch_array($VERCODEQ)) {
              $status = $row["code_status"];
            }
              // we will first insert the user main information into the database: i.e. password, username, etc. //
              $account_code = random_strings(8);
              $APPROVE = "INSERT INTO user (username, password, email, account_id, account_code, active, status, registered_date, verification_code)
                          SELECT username, password, email, account_id, '".md5($account_code)."', 1, $status, $now, verification_code
                          FROM registration_list
                          WHERE registration_id = ?";
              $stmt = $con->prepare($APPROVE);
              $stmt->bind_param("i", $val); // Problem around here //
              $stmt->execute();
            if (($stmt->error) == FALSE) {

我希望这个过程不会有任何问题,因为我已经检查了所有内容,并且在我看来没有任何问题。

最佳答案

重新格式化您的代码,使其更清晰、更容易理解,我们现在:

<?php
    // we will only start the session with session_start() IF the session isn"t started yet //
    if (session_status() == PHP_SESSION_NONE) 
    {
        session_start();
    }
?>

<?php
    // including the conn.php to establish connection with database //
    include "conn.php";
?>

<?php
    // Begin of the function: Registration List's Verification Form: Registration //

    // First we check the form has submitted or not //
    if (isset($_POST['submit-list-reg'])) 
    {
        // If it is, then we will retreive data from the input forms //
        $regid = $_POST["regid"];
        $reg_acccode = mysqli_real_escape_string($con, $_POST['reg-acccode']);
        $reg_pw = mysqli_real_escape_string($con, $_POST['reg-pw']);

        // Taking the current time //
        date_default_timezone_set("Etc/GMT-8");
        $now = date("Y-m-d H:i:s");

        // Variable to store Error Message //
        $error = '';

        // Alphanumeric Generator //
        function random_strings($length_of_string) 
        {
            // String of all alphanumeric character
            $str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

            // Shufle the $str_result and returns substring
            // of specified length
            return substr(str_shuffle($str_result), 0, $length_of_string);
        }

        // Sorting out the query related to the function //

        // Verify the user is an admin or not //
        $VERFYADMIN = "SELECT * FROM user
                 WHERE status = 2 AND active = 1 AND account_code = '".md5($reg_acccode)."' AND password = '".md5($reg_pw)."'";
        $VERFYADMINQ = mysqli_query($con, $VERFYADMIN);

        //***BEGIN OF PROCESS***//
        if (mysqli_num_rows($VERFYADMINQ) < 1) 
        {
            // if the admin is not verified, then inform the user and send him back to admin panel //
            echo "<script>alert('ALERT: Information unable to be verified. Please try again.');";
            echo "window.location.href='admin_panel.html';</script>";
            exit(0);
        }
        else
        {
            // begin the process of registration //
            while(list($key,$val) = @each ($regid)) 
            {
                // Now to verify the user's legitimacy //
                // Take the user's vercode into variable first //
                $USERVERCODE = "SELECT * FROM registration_list WHERE registration_id = $val AND verified = 0";
                $USERVERCODEQ = mysqli_query($con, $USERVERCODE);

                if (mysqli_num_rows($USERVERCODEQ) < 1) 
                {
                    // if we are unable to retrieve the data of the registering user then something must gone wrong //
                    echo "<script>alert('WARNING: Unable to retrieve the data. Please try again.');";
                    echo "</script>";
                }
                else
                {
                    while ($row = mysqli_fetch_array($USERVERCODEQ)) 
                    {
                        $vercode = $row["verification_code"];
                    }

                    // since we got the value of the vercode then we start to define the query //
                    $VERCODE = "SELECT * FROM verification_code WHERE verification_code = $vercode AND code_active = 1";
                    $VERCODEQ = mysqli_query($con, $VERCODE);

                    if (mysqli_num_rows($VERCODEQ) < 1) 
                    {
                        // if we are unable to retrieve the data of the registering user then something must gone wrong //
                        echo "<script>alert('WARNING: Unable to retrieve the info of VERCODE. Please try again.');";
                        echo "</script>";
                    }
                    else
                    {
                        while ($row = mysqli_fetch_array($VERCODEQ)) 
                        {
                            $status = $row["code_status"];
                        }

                        // we will first insert the user main information into the database: i.e. password, username, etc. //
                        $account_code = random_strings(8);
                        $APPROVE = "INSERT INTO user (username, password, email, account_id, account_code, active, status, registered_date, verification_code)
                                    SELECT username, password, email, account_id, '".md5($account_code)."', 1, $status, $now, verification_code
                                    FROM registration_list
                                    WHERE registration_id = ?";
                        $stmt = $con->prepare($APPROVE);
                        $stmt->bind_param("i", $val); // Problem around here //
                        $stmt->execute();

                        if (($stmt->error) == FALSE) 
                        {

这里有几件事是我个人不会做的。正如已经提到的,最好避免直接在 SQL 查询中使用用户输入提供的变量,甚至是 MD5 变量。

设置 $val 变量的“while(list($key,$val) = @each ($regid))”行有一个 & 符号来抑制任何错误消息,这反过来可能会导致您出现进一步的问题向下。最好不要抑制这些消息,而是要找出它们发生的原因,这可能是导致非数字值传递给“bind_param”函数的原因。我还会在函数中使用单引号而不是双引号。

关于php - 检查了查询、数据库、表名、列名等,还是不明白为什么会出现这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55462828/

相关文章:

php - mysql邮政编码搜索

php - 尝试显示存储在数据库中的图像时损坏的 img 链接

PHPMailer - OpenSSL 错误

php - 无法在 Ubuntu 上运行 gettext (php)

php - X-Sendfile具体配置.htaccess问题

php - 有没有办法在不到 2 分钟的时间内从 txt 文件更新 12000 多行?

php - mysqli 最后插入 id

php - 为什么 Laravel 或 Beanstalkd 会跳槽?

php - 账户激活 PHP

php - 无法连接到 MySQL : Unknown database 'logindb'