简体   繁体   中英

PHP-MySQL error inserting data

How to fix this

Catchable fatal error: Object of class PDOStatement could not be converted to string in.

My PHP code:

$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');

You override your variable $a

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

Try to give a different name

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

Using meaningful names for variables ($stmt instead of $a, for example, which would have avoided the conflict)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM