Search code examples
phpmysqlmysqlibindparam

Function prepare query


I'm doing, or trying to do, a database project for the university, but when registering a user this error appears:

Fatal error: Call to a member function bind_param() on a non-object in (...)

Initially I wrote

$insert = $db->prepare("INSERT INTO customer (name, email, phonenumber, adress, password) VALUES (?, ?, ?, ?, ?");

But then I changed to well, you can see in the code.

<?php
require 'db/connect.php';
require 'functions/security.php';

if(!empty($_POST)) {

    if(isset($_POST['name'], $_POST['email'], $_POST['address'], $_POST['phone'], $_POST['password'])) {

        $name = trim($_POST['name']);
        $email `enter code here` = trim($_POST['email']);
        $phone = trim($_POST['phone']);
        $address = trim($_POST['address']);
        $password  = trim($_POST['password']);

        if(!empty($name) && !empty($email) &&!empty($phone) && !empty($address) &&!empty($password)){
                $insert = $db->prepare("INSERT INTO customer VALUES (?, ?, ?, ?, ?");
                $insert->bind_param('ssiss', $name, $email, $phone, $address, $password);
                //$insert->close();

            if($insert->execute()){
                print_r("Done");
                die();
            }            
        }        
    }
}
?>

Solution

  • Call to a member function in query's means that the query couldn't get executed because it contains an error.

    In this case, you didn't closed the VALUES (). Change $insert = $db->prepare("INSERT INTO customer VALUES (?, ?, ?, ?, ?"); to $insert = $db->prepare("INSERT INTO customer VALUES (?, ?, ?, ?, ?)");

    Make sure you do check if an error could get executed. Example of checking if an query could get executed:

    $query = $this->_db->prepare("SELECTTEST name FROM user"); //.. Bad query (false)
    if(!$query) //.. If the query is false.
    {
         trigger_error('Query couldn\'t get executed');
    }
    

    If this solved your error, I will really appreciate that you vote my answer as answer.