2015-11-20 104 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));