我有一个网站在几个小时内完成一个项目,我想解决这个令人讨厌的错误。
我有一个向build_transaction.php发送数据的表单,但我收到错误:
致命错误:在布尔值中调用成员函数bind_param() C:\ xampp \ htdocs \ GigaWatt \ build_your_rig \ build_transaction.php在线 29。
我的HTML页面的代码是:
<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>
我的PHP代码是:
<?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.";
?>
编辑:现在一切正常,我在查询中调用数据库名称&#34; Gigawatt&#34;,实际的表名是&#34; specs&#34; ...我觉得很傻,借口我
答案 0 :(得分:2)
原因是$statement
在返回false
时变为布尔值。为什么?可能是格式化,可能是拼写错误。要检查预准备语句是否返回true
,请始终在if
语句中折叠执行。我也正确地格式化了查询,但可能需要进一步修改。
如果返回false
,它也会回显错误。
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;
}