0

So I'm having trouble finding a solution to my problem, which seems like an odd one to begin with to me. Whenever I try to call bind_param on my mysqli prepared statement I'm told "Call to member function bind_param() on non-object." I have a loop to print out the methods of the class, and it has bind_param in the list.

Register.php File

<?php
include('database.php');
if(isset($_SESSION['user'])){
    die("You are already logged in ".$_SESSION['user'].", you can't register right now.");
}
register($_POST['username'],$_POST['password'],$_POST['pin'],$_POST['fname'],$_POST['lname'],$_POST['email']);
function register($u,$p,$pi,$f,$l,$e){
    /*if($u === "" or $u==="Admin" or $u==="Administrator"){
        die("You cannot choose this username");
    }*/ 
    if($pi < 0 or $pi > 99999){
        die("Invalid Pin number");
    }
    if($f === "" or $l === ""){
        die("Invalid first or last name");
    }
    if($e === ""){
        die("Invalid email");
    }
    global $conn;
    $prep = $conn->prepare("SELECT email FROM users WHERE username=?");
    $meth =  get_class_methods($prep);
    foreach ($meth as $method_name) {
        echo "$method_name<br>";
    }
    $prep->bind_param("s",$u);
    $prep->execute();
    if($prep->error){
        die("User creation failed with the following error: ".$prep->error());
    }
    if($prep->num_rows > 0){
        die("User already exists");
    }
    $prep->close();


    $prep2 = $conn->prepare("INSERT INTO users VALUES(username, password, pin, fname, lname, email,coin) (?,?,?,?,?,?,0)");
    $prep2->bind_param("ssisss",$u,$p,$pi,$f,$l,$e);
    $prep2->execute();
    if(!$prep2->error()){
        die("User created successfully");
    }else{
        die("User creation failed with the following error: ".$prep->error());
    }
    $prep2->close();
}?>

The database.php file it includes

    <?php
$ip = "xo6.x10hosting.com";
$username = "-snip-";
$password = "-snip-";
$database = "-snip-";
$conn = new mysqli($ip, $username, $password, $database);
if ($conn->connect_error or mysqli_connect_error()) {
    die("Connection failed: " . $conn->connect_error);
}
if($conn->error){
    die("Mysql Error: ".$conn->error);
}?>

I don't get any errors from the database.php file so I don't think the problem is there.

end360
  • 9
  • 2
  • Are you sure it's the first bind_param throwing the error, and not the second? – aynber Aug 29 '16 at 20:57
  • It was the first but I rewrote it, something is up with the SQL syntax in the second `You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(?,?,?,?,?,?,0)' at line 1` – end360 Aug 31 '16 at 13:42
  • The second, you have the order of the query wrong. It's `INSERT INTO table (list,of,columns) VALUES (?,?,?)` – aynber Aug 31 '16 at 14:32

1 Answers1

0

You make a mistake in that block of code:

if(!$prep2->error()){
        die("User created successfully");
    }else{
        die("User creation failed with the following error: ".$prep->error());
    }

You try to call $prep->error() however $prep was closed before.

Andrej
  • 7,474
  • 1
  • 19
  • 21
  • In that block I tried calling $prep2->error(), not $prep->error(), however I meant to remove the parentheses since error is a variable not method. – end360 Aug 30 '16 at 16:16
  • Look at this line die("User creation failed with the following error: ".$prep->error()); – Andrej Aug 30 '16 at 17:08