简体   繁体   English

检查查询,数据库,表名,列名等。仍然不知道为什么会发生此错误

[英]Checked the query, database, table names, column names, etc. Still have no idea why this error occurred

The error that I occurred: 我发生的错误:

Fatal error: Call to a member function bind_param() on boolean in C:\\wamp64\\www\\APU\\SDP\\reg-list-function.php on line 82 致命错误:在第82行的C:\\ wamp64 \\ www \\ APU \\ SDP \\ reg-list-function.php中调用boolean上的成员函数bind_param()

I'm writing a php script where the Admins are able to approve the registration of the user. 我正在编写一个php脚本,管理员可以批准用户的注册。 I've checked through the formats of my database, column names, and even query, and still I've no idea why this error pops out. 我已经检查了我的数据库格式,列名,甚至查询,但我仍然不知道为什么会出现这个错误。 Any help or suggestions will be appreciated! 任何帮助或建议将不胜感激!

<?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) {

I expect the process will be no issue at all as I've checked everything and nothing seems wrong to me. 我希望这个过程完全没有问题,因为我已经检查过所有内容,而且我没有任何错误。

Reformatting your code to make it more legible and easier to understand, we now have: 重新格式化代码以使其更易读,更易于理解,我们现在拥有:

<?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) 
                        {

In here are several things that I wouldn't personally do. 在这里有几件我个人不会做的事情。 As has been mentioned, using variables supplied by user input, even MD5 ones, directly in SQL queries should be best avoided. 如前所述,应该最好避免在SQL查询中直接使用用户输入提供的变量,甚至是MD5。

The line "while(list($key,$val) = @each ($regid))", which sets the $val variable has an ampersand to suppress any error messages, this in turn could be causing you issues further down. “while(list($ key,$ val)= @each($ regid))”这一行设置$ val变量有一个&符号来抑制任何错误消息,这反过来可能导致你进一步问题。 It's best not to suppress these messages, but to find out why they are occurring, this could be the cause of a non numeric value being passed to your "bind_param" function. 最好不要压制这些消息,但要找出它们发生的原因,这可能是将非数值传递给“bind_param”函数的原因。 I'd also use single quotes instead of double quotes with the function as well. 我也会使用单引号而不是双引号。

解决了我用这种格式改变了包含字符串值的变量 - >'“。$ variable。”'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM