Jeremy
Jeremy

Reputation: 374

PHP Sqlite and PDO Insert

I am developing a simple money tracking web app on my Mac using SQLite and PHP. I am having a problem inserting data into the sqlite db using parameters with the insert statement. This is not a permissions problem because I have been able to write a static insert statement and it executes properly. Here is my code:

try
{
$databaseins = new PDO("sqlite:/datastores/trackmoney.db")  or die("Could not open database"); 
$databaseins->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO Transactions 
    (TransactionID, TransactionName, Expense, Description, TransactionDate, DateEntered, RecordedBy) 
VALUES (:TransactionID,:TransactionName,:Expense,:Description,:TransactionDate,:DateEntered,:RecordedBy);";
// below is a static insert that works
//$sql = "INSERT INTO Transactions (TransactionID, TransactionName, Expense, Description, TransactionDate, DateEntered, RecordedBy) VALUES ('$guidTransactionID','Test',12.12,'test','3/2/2012','12/12/2012',1);";
$q = $databaseins->prepare($sql);
$q->bindParam(':TransactionID', $guidTransactionID, SQLITE3_TEXT);
$q->bindParam(':TransactionName', $strTransName, SQLITE3_TEXT);
$q->bindParam(':Expense', $fltTransAmount, SQLITE3_FLOAT);
$q->bindParam(':Description', $strTransDescrip, SQLITE3_BLOB);
$q->bindParam(':TransactionDate', $strDateOfTrans, SQLITE3_TEXT);
$q->bindParam(':DateEntered', "1/1/2013", SQLITE3_TEXT);
$q->bindParam(':RecordedBy', 1, SQLITE3_INTEGER);

$count = $q->execute() or die($databaseins->errorInfo());
print("<b>" + $count + "</b>");
$databaseins = null;
}
catch(PDOException $e)
{
echo $e->getMessage();//this getMessage throws an exception if any 
}

I do not receive any error messages. Note, that all of the values of the variables are set and have a value.

Thanks for any help.

Upvotes: 1

Views: 2133

Answers (1)

Jeremy
Jeremy

Reputation: 374

Thanks to andrewsi and JvdBerg for your help. The fix to my problem was to use bindValue instead of bindParam and change SQLITE3_BLOB to SQLITE3_TEXT.

Upvotes: 2

Related Questions