【问题标题】:PHP-MySQL error inserting dataPHP-MySQL错误插入数据
【发布时间】:2015-11-20 13:30:46
【问题描述】:

如何解决这个问题

可捕获的致命错误:不能是 PDOStatement 类的对象 转换成字符串。

我的 PHP 代码:

$a = $_POST['id'];
$b = $_POST['title'];
$c = $_POST['cat'];
$d = $_POST['cop'];
$e = $_POST['stat'];

$sql = "INSERT INTO books (book_id, book_title, book_category, no_copies, status) VALUES (:a,:b,:c,:d,:e)";
$a = $db->prepare($sql);
$a->execute(array(':a'=>$a, ':b'=>$b, ':c'=>$c, ':d'=>$d, ':e'=>$e));
header('Location: books.php');

【问题讨论】:

标签: php pdo


【解决方案1】:

你覆盖你的变量$a

$a = $_POST['id']; // assign here
$a = $db->prepare($sql);// override here

试着给一个不同的名字

$smt = $db->prepare($sql);
$smt->execute(array(':a'=>$a,':b'=>$b,':c'=>$c,':d'=>$d,':e'=>$e));
header("location: books.php");

【讨论】:

    【解决方案2】:

    对变量使用有意义的名称(例如,$stmt 而不是 $a,这样可以避免冲突)

    $stmt = $db->prepare($sql);
    $stmt->execute(array(':a'=>$a,':b'=>$b,':c'=>$c,':d'=>$d,':e'=>$e));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 2013-03-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多