当我运行我的代码时,我会得到以下错误:
Error: Call to a member function bind_param() on a non-object我搜索了几个小时这个问题,我不知道问题出在哪里。我知道这个主题已经发布了很多次,但我是php初学者,我不明白为什么这不起作用。
这是我使用的代码:
班级:
class Item {
private $iname;
function __construct($name)
{
$this->iname = $name;
}
public function addItem()
{
global $mysqli, $db_table_prefix;
$stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."items (
iname,
VALUES (
?
)");
$stmt->bind_param("s", $this->iname);
$stmt->execute();
$inserted_id = $mysqli->insert_id;
$stmt->close();
}
}并张贴表格页:
require_once("models/config.php");
if(!empty($_POST))
{
$item_name = trim($_POST["iname"]);
$item = new Item($item_name);
$item->addItem();
}
require_once("models/header.php");
echo "
<body onload='initialize()'>
<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h>Add new Item</h>
<div id='main'>
<div id='regbox'>
<form name='newItem' action='".$_SERVER['PHP_SELF']."' method='post'>
<p>
<label>Item description:</label>
<input type='text' name='iname' />
</p>
<input type='submit' name='Submit' value='Add Item'/>
</form>
</div>
</div>
</div>
</div>
</body>
</html>";
?>发布于 2014-02-07 11:16:23
有一个SQL语法错误:
INSERT INTO ".$db_table_prefix."items (
iname,
VALUES (
?
)这应该是:
INSERT INTO ".$db_table_prefix."items (
iname)
VALUES (
?
)由于语法错误,没有创建$stmt对象,因此出现了错误Call to a member function bind_param() on a non-object。
如果遇到问题,请始终输出MySQL错误消息:
echo $mysqli->error;https://stackoverflow.com/questions/21625990
复制相似问题