PHP-MySQL 插入数据时出错
PHP-MySQL error inserting data
如何解决这个问题
Catchable fatal error: Object of class PDOStatement could not be
converted to string in.
我的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');
为变量使用有意义的名称(例如,$stmt 而不是 $a,这样可以避免冲突)
$stmt = $db->prepare($sql);
$stmt->execute(array(':a'=>$a,':b'=>$b,':c'=>$c,':d'=>$d,':e'=>$e));
你覆盖了你的变量$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");
如何解决这个问题
Catchable fatal error: Object of class PDOStatement could not be converted to string in.
我的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');
为变量使用有意义的名称(例如,$stmt 而不是 $a,这样可以避免冲突)
$stmt = $db->prepare($sql);
$stmt->execute(array(':a'=>$a,':b'=>$b,':c'=>$c,':d'=>$d,':e'=>$e));
你覆盖了你的变量$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");