php - Function prepare query

403

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();
            }            
        }        
    }
}
?>
914

Answer

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.

People are also looking for solutions to the problem: php - Redirect the Page after Form is Submitted - Javascript

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.