gpt4 book ai didi

php - Mysql:调用非对象上的成员函数bind_param()

转载 作者:行者123 更新时间:2023-11-29 00:12:38 25 4
gpt4 key购买 nike

使用usercake,我在注册页面添加了一个额外的字段来记录用户的兴趣并将其存储在一个名为interests的表中。

因为我没有存储在同一个表中,所以我需要创建一个单独的查询,如下所示。请注意,用户提供的数据正在转换为数组。

这是我的查询:

//add interests1
$interestsArray = explode(',', $this->interests);
$interests1 = $interestsArray[0];
$interests2 = $interestsArray[1];
$interests3 = $interestsArray[2];
$interests4 = $interestsArray[3];
$interests5 = $interestsArray[4];
$this->interests1 = sanitize($interests1);
$this->interests2 = sanitize($interests2);
$this->interests3 = sanitize($interests3);
$this->interests4 = sanitize($interests4);
$this->interests5 = sanitize($interests5);
$stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."interests (
interest
)
VALUES (
?
)");
$stmt->bind_param("s", $this->interests1);
$stmt->execute();
$stmt->close();

但是我收到以下错误:

Fatal error: Call to a member function bind_param() on a non-object in /path/models/class.newuser.php on line 218

但是代码中没有第 218 行……让我感到困惑。我希望最终将每个变量存储在单独行的列中的数组中。你们有什么建议?

如果有帮助的话,这里是整个类(class)的链接:linktocode

最佳答案

$stmt 可能是 FALSE 由于某些原因的数据库错误,例如没有连接,USE 没有选择数据库,表也不存在。检查数据库错误并将它们写入日志。

if(mysqli_connect_errno())
{
// log somewhere mysqli_connect_error()
}
else
{
if($stmt = $mysqli->prepare("INSERT ..."))
{
$stmt->bind_param("s", $this->interests1);
$stmt->execute();
}
else
{
// log somewhere $mysqli->error;
}
}

关于php - Mysql:调用非对象上的成员函数bind_param(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24336741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com