将多个参数传递给 PHP 函数时出错(类注释的对象无法转换为字符串错误)


Error passing multiple parameter to PHP function (Object of class comments could not be converted to string error)

这可能非常简单,但我仍处于PHP的学习阶段。我创建了一个类,可用于将参数从另一个页面传递到它。我已经在将一个参数传递给它时测试了该函数,它可以工作,但似乎抛出了"可捕获的致命错误:类注释的对象无法转换为字符串"错误。

class comments{
 public function insertcomment($postid, $postcomment){
    include('db-conx.php');
    echo "<br/>$postid<br/>";
    echo "$postcomment";
    $userid = $_SESSION['userid'];
    date_default_timezone_set('America/New_York');
    $date = date('M d,Y h:i:s');
    $insertcomm = "INSERT INTO comments (`userid`,`postid`,`commentdate`,`commentcontent`) VALUES         (?, ?, ?,?)";
    $stmt = $conn->stmt_init();
    if ($stmt->prepare($insertcomm))
    {
        //$userid = $_SESSION['userid'];
        $stmt->bind_param("ssss", $userid,$postid,$date,$postcomment);
        echo "$postcomment";
        $stmt->execute();
        echo "True";
    }
    else{
        echo "false";
    }
}
}

调用函数并分页其他页中的其他参数

$postcomment = new comments();
$returned=$postcomment->insertcomment($postid, $postcomment);

一天调试,运气不佳。有什么帮助吗?

你使用变量$postcomment两次。我假设您在通过创建新的注释对象覆盖它之前有一个字符串?请尝试:

$comment = new comments();
$returned = $comment->insertcomment($postid, $postcomment);
好吧,

大家觉得很笨。事实证明,我在调用它上面的代码中重新分配了$postcontent。可能是一个迹象,我需要更多休息,因为我整晚都在为此作斗争。

我认为您可能错误地将对象传递回该方法。 $postcomment是 comments() 的一个实例,但随后您将$postcomment作为参数传递给插入注释?