0

I have a website due a project within a few hours and I would like to fix this annoying error I have.

I have a form that sends data to build_transaction.php yet I get the error:

Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\GigaWatt\build_your_rig\build_transaction.php on line 29.

The code for my HTML page is:

        <form action="build_transaction.php" method="post">

            <label for="cpu">Processor: </label>
            <input type="text" id = "cpu" name = "cpu" required><br>

            <label for="gpu">Graphics Card: </label>
            <input type="text" id = "gpu" name = "gpu" required><br>

            <label for="psu">Power Supply (Watts): </label>
            <input type="number" id = "psu" name = "psu" value = "0" min = "300" max="2000" required><br>

            <label for="motherboard">Motherboard: </label>
            <input type="text" id = "motherboard" name = "motherboard" required><br>

            <label for="mouse">Mouse: </label>
            <input type="text" id = "mouse" name = "mouse" required><br>

            <label for="keyboard">Keyboard: </label>
            <input type="text" id = "keyboard" name = "keyboard" required><br>

            <label for="monitor">Monitor: </label>
            <input type="text" id = "monitor" name = "monitor" required><br>

            <label for="price">Price: </label>
            <input type="number" id = "price" name = "price" required><br>     

            <input id="submit" type="submit" value="Add Record">

        </form>

and the code for my PHP is:

<?php
/* Include "configuration.php" file */
require_once '../database_and_table_setup/configuration.php';

$cpu = filter_input(INPUT_POST, "cpu");
$gpu = filter_input(INPUT_POST, "gpu");
$psu = filter_input(INPUT_POST, "psu");
$motherboard = filter_input(INPUT_POST, "motherboard");
$mouse = filter_input(INPUT_POST, "mouse");
$keyboard = filter_input(INPUT_POST, "keyboard");
$monitor = filter_input(INPUT_POST, "monitor");
$price = filter_input(INPUT_POST, "price");

/* Connect to the database */
$dbConnection = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);
if (mysqli_connect_errno())
{
die("Failed to connect to the database ".$dbName);
}

/* Perform query */
$query = "INSERT INTO gigawatt (cpu, gpu, psu, motherboard, mouse, keyboard, monitor , price) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$statement = $dbConnection->prepare($query);
$statement->bind_param('ssissssi', $cpu, $gpu, $psu, $motherboard, $mouse, $keyboard, $monitor, $price);         /* Valid binding types are: s = string, i = integer, d = double,  b = blob */
$statement->execute();

/* Manipulate the query result */
/* We do not manipulate the query result. Instead, we can provide feedback that the record has been added */
if (mysqli_affected_rows($dbConnection) > 0) 
{
echo "<p>Record successfully added to database.</p>";   
}
else
{
echo "<p>Record not added to database.</p>";
}

/* Close the database connection */
mysqli_close($dbConnection);



/* Provide a link for the user to proceed to a new webpage or automatically redirect to a new webpage 
echo "<a href=" . $siteName . "/display_all_records_students.php>Click here to add another record</>";*/

//$newURL = "display_all_records.php";
//header('Location: ' . $newURL);

echo "Successfully Added to DB.";
?>

EDIT: Everything is working now, I was calling the DB name in my query "Gigawatt", the actual table name was "specs"... I feel so silly, excuse me.

  • I think that filter_input function returns boolean of your data instead of values. Try to do without filter_input – ManuRGDev Dec 15 '15 at 00:12
  • Okay I'll try, thank you! :) – RidiculousBeans Dec 15 '15 at 00:13
  • It's right there: "Call to a member function bind_param() on boolean." What are you calling `bind_param()` on? When is it getting assigned a value, and is it getting a boolean value instead of what you expect? – miken32 Dec 15 '15 at 00:23
  • 1
    Possible duplicate of [Call to a member function on a non-object](http://stackoverflow.com/questions/54566/call-to-a-member-function-on-a-non-object) – miken32 Dec 15 '15 at 00:28

1 Answers1

2

The reason is that $statement becomes a boolean value when it returns false. Why? Could be formatting, could be typos. To check whether the prepared statement returns true, always fold the execution in an if statement. I formatted the query properly too, but it might need further revision.

It also echos the error, if false is returned.

if($statement=$dbConnection->prepare("INSERT INTO `gigawatt` (`cpu`, `gpu`, `psu`, `motherboard`, `mouse`, `keyboard`, `monitor`, `price`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"))
{
    $statement->bind_param('ssissssi', $cpu, $gpu, $psu, $motherboard, $mouse, $keyboard, $monitor, $price);
    $statement->execute();
}
else
{
    echo $dbConnection->errno. "Message: ".$dbConnection->error;
}
Fredrik
  • 764
  • 1
  • 6
  • 22