0

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>
5
  • 1
    Can you post the form code, looks like $username might not be set Commented Apr 1, 2014 at 23:04
  • I see no bind_param() on line 14. Are you sure you posted the proper code? Commented Apr 1, 2014 at 23:05
  • it could be stemming from your $db init line, since the query seems to look ok Commented Apr 1, 2014 at 23:05
  • Its this code, sorry $check->bind_param(":username",$username); Commented Apr 1, 2014 at 23:07
  • @user43268 Have you not read Bill's answer? Commented Apr 1, 2014 at 23:38

2 Answers 2

3

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().

Sign up to request clarification or add additional context in comments.

5 Comments

I don't think the OP saw your answer, or even knows how to fix his/her code. This is the most likely answer. Plus, OP states in a comment that he/she used the same code before, but without session_start();, hm, I doubt that very much, that the code "used" to work.
@Fred-ii- When did I state it ever worked? I stated that the only different between this code and the one I have is that it doesn't include session start, no big deal. And yes I did see this answer and took it into account, it actually helped and I am working on fixing it, and yes I don't know how to quiet fix it since you said it.
In your comment @user43268 Or, did I dream that up?
@Fred-ii- I never said it worked... I said the only difference is the session start...
Ok. Nonetheless, you're using the wrong type of placeholders. @user43268
1

always have this line before mysqli connect

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

3 Comments

Alright, I input that and I got this Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':username' at line 1' in /var/www/html/db/register.php:14 Stack trace: #0 /var/www/html/db/register.php(14): mysqli->prepare('SELECT * FROM u...') #1 {main} thrown in /var/www/html/db/register.php on line 14
most likely you are using query instead of prepare in the real code you run, not one you posted here
The code I have here is the same as the one i'm using. The only difference is here I didn't include the session start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.