简体   繁体   中英

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

I'm writing a php script where the Admins are able to approve the registration of the user. 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.

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. 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. I'd also use single quotes instead of double quotes with the function as well.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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