2015-11-20 89 views
-5

如何解决这个PHP,MySQL错误插入数据

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'); 
+0

[以下是如何绑定参数(http://php.net/manual/es/pdostatement.bindparam.php) –

+1

你覆盖你的变量' $ a'在这里:'$ a = $ db-> prepare($ sql);';做一些基本的调试。 – Rizier123

回答

2

你重写你的变量$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"); 
0

使用有意义的名称变量($语句而不是$一个,例如,这将有避免冲突)

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