Using SQLite and PDO. I'm quite new to PDO so I could use some help here. I get the error:
SQLSTATE[HY000]: General error: 25 bind or column index out of range
Here is my code:
// db.php
function dbh($sql, $db = "", $nsx = "") {
$db = ($db) ? $db : "mydb";
$db = $db.".sqlite";
try {
$dbh = new PDO("sqlite:$db");
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// id, title, desc, cost, album, version, added
$dbh->exec("CREATE TABLE IF NOT EXISTS `products` (
`id` INTEGER PRIMARY KEY,
`title` TEXT,
`desc` TEXT,
`cost` TEXT,
`album` TEXT,
`version`TEXT,
`added` DATETIME
)");
//$mem = new PDO('sqlite::memory');
//$mem->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$res = $dbh->prepare($sql);
//var_dump($nsx);
$res->execute($nsx);
if (!is_array($nsx)) {
return $res->fetchAll();
}
}
catch(PDOException $e) {
return $e->getMessage();
}
}
// products.php
$sql = "INSERT INTO `products` (title, desc, cost, album, version, added) VALUES (:title,:desc,:album,:version,:cost,:date)";
$nsx = array(
':title' => $i['addon']['title'][$k],
':desc' => $i['addon']['desc'][$k],
':cost' => $i['addon']['cost'][$k],
':album' => $album,
':version' => '1.0',
':added' => $date,
);
$dbh = dbh($sql,"",$nsx);
You bind :added
, but did not specify it in the VALUES list: (:title,:desc,:album,:version,:cost,:date)
. Instead you specified :date
which is not bound by your array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With