简体   繁体   中英

Mysqli PHP Fatal error: Call to a member function bind_param() on a non-object

I'm working on a dashboard for someone and i'm getting an error. I'm creating a registration page and what it does is it looks to see if the users already in the database, if it is then it allows them to complete the registration, but when I submit, it creates this error Fatal error: Call to a member function bind_param() on a non-object on line 8

Heres the PHP code:

<?php
include"core/config.php";

if(isset($_POST['submit']))
{
  $username = $_POST['username'];
  $email = $_POST['email'];

  $check = $db->prepare("SELECT * FROM users WHERE username = :username");
  $check->bind_param(":username",$username);
  $check->execute();
  echo $check->error;

  $emailCheck = null;
  $usernameCheck = null;
  while($row = $check->fetch())
  {
    $emailCheck = $row['email'];
    $usernameCheck = $row['username'];
  }
  if(!$email == $emailCheck)
  {
    echo 'Email doesn\'t exist!';
  }else{

    if(!$username == $usernameCheck)
    {
      echo 'Username doesn\'t exist!';
    } else
    {
      $query = $db->prepare("SELECT * FROM users WHERE username = :username AND email = :email");
      $query->bind_param(":username", $username);
      $query->bind_param(":email", $email);
      $query->execute();
       $_SESSION['username'] = $username;
      echo 'You have logged in, you will be redirected.';
      echo '<META HTTP-EQUIV="REFRESH" CONTENT="5;URL=page to set password.">';
    }
  }
}
?>

Form code:

<form class="no-margin" method="post">
                <fieldset>
                    <div class="form-group no-margin">
                        <label for="username" >Username</label>

                        <div class="input-group input-group-lg">
                                <span class="input-group-addon">

                                </span>
                            <input id="username" type="username" name="username" class="form-control input-lg" placeholder="Username">
                        </div>

                    </div>

                    <div class="form-group">
                        <label for="Email" >Email</label>

                        <div class="input-group input-group-lg">
                                <span class="input-group-addon">

                                </span>
                            <input id="email" type="email" name="email" class="form-control input-lg"
                                   placeholder="Email">
                        </div>

                    </div>
                </fieldset>
                <div class="form-actions">
                    <button type="submit" name="submit" class="btn btn-block btn-lg btn-danger">
                        </span>
                        <small>Submit</small>
                    </button>
                    <div class="forgot"><a href="not important">Not a member? Join here!</a></div>
                </div>
            </form>

I assume you're using ext/mysqli because bind_param() is part of that extension.

Mysqli doesn't support named parameters. You have to use positional parameters with the ? placeholder.

Also Mysqli's bind_param() has a different usage than the way you're using it. You have to bind all params, and the first argument is a "control" argument. I suggest you read the documentation for examples.

PDO supports named parameters, and you can bind one parameter per call to bindParam() .

mysqli连接之前总是有这条线

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

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