可恢复的致命错误:类 PDOStatement 的对象无法转换为字符串 [英] Recoverable fatal error: Object of class PDOStatement could not be converted to string

查看:81
本文介绍了可恢复的致命错误:类 PDOStatement 的对象无法转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP 将数据发送到数据库,但是当我使用 execute() 执行查询时,出现错误提示

I am trying to send data to a database using PHP but when I execute the query using execute() I get an error that says

可恢复的致命错误:第 12 行 C:\xampp\htdocs\Code Sharing Website\submit_snippet.php 中的类 PDOStatement 的对象无法转换为字符串

Recoverable fatal error: Object of class PDOStatement could not be converted to string in C:\xampp\htdocs\Code Sharing Website\submit_snippet.php on line 12

这是我的代码

include 'includes/db.php';

if(isset($_POST['title']) && isset($_POST['snippet'])) {
  $title = $_POST['title'];
  $snippet = $_POST['snippet'];

  $snippet = $db->prepare("INSERT INTO all_snippets (snippet_name, snippet_body) VALUES (:title, :snippet)");
  $snippet->execute(array(
    ':title' => $title,
    ':snippet' => $snippet
  ));
} else {
  echo "Error: Please fill out all fields";
}

推荐答案

您正在将 snippet 变量重新分配给 PDO 对象,然后尝试在您的 execute 中使用它.其中一个变量需要重命名.

You are reassigning your snippet variable to a PDO object and then try to use that in your execute. One of those variables needs to be renamed.

$snippet = $_POST['snippet']; 更改为 $snippetPost = $_POST['snippet']; 和您的 execute 应该可以解决这个问题.

Changing $snippet = $_POST['snippet']; to $snippetPost = $_POST['snippet']; and your execute to this should fix it.

  $snippet->execute(array(
    ':title' => $title,
    ':snippet' => $snippetPost
  ));

这篇关于可恢复的致命错误:类 PDOStatement 的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆