Can't INSERT into mysql via PDO

jrgilman

I've been banging my head against this PHP insert code for the last day or so. I can't seem to find the problem and it persists after 2 complete re-do's of the code. The problem is pretty straightforward, it won't insert anything, and I've cut down on the possibilities by simple directly inputting values for the variables rather than using GET to obtain them from the HTML via JS. Maybe someone on here can spot what is going on. I know PDO is definitely working though, because I am able to obtain info from the mysql table.

<?php

$tbl = 'transactions';
$acc = 'blah1';
$date = '2014-07-01';
$cp = 'counterparty';
$ctg = 'category';
$dbt = 1.00;
$crd = 0.00;

$user = "root";
$pass = #######; // Note: the password is actually in the file on my side.

try {
    $con = new PDO("mysql:host=localhost;dbname=budget;charset=utf8",$user,$pass);
    $con -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

$sql = $con -> prepare("INSERT INTO transactions VALUES (:account, :date, :counterparty, :category, :debit, :credit)");

$sql -> bindValue(':account', $acc, PDO::PARAM_STR);
$sql -> bindValue(':date', $date, PDO::PARAM_STR);
$sql -> bindValue(':counterparty', $cp, PDO::PARAM_STR);
$sql -> bindValue(':category', $ctg, PDO::PARAM_STR);
$sql -> bindValue(':debit', strval($dbt), PDO::PARAM_STR);
$sql -> bindValue(':credit', strval($crd), PDO::PARAM_STR);

$sql -> execute();

$dbh = NULL;
?>

Any help would be appreciated. Thanks.

jrgilman

I answered this in the comments of my post, but I'm gonna post it up as an answer so that it is more visible:

Well the code works now, I added this to the JS: xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) {}} in an attempt to see what was going on with the try-catch blocks, and suddenly the code started to work. I'm not sure I understand why this suddenly fixed the problem? Could someone care to explain.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert multiple values via PHP PDO into MySQL database

From Dev

Can't write on MySQL PDO database

From Dev

Converting MySql Insert To PDO

From Dev

INSERT INTO array with MySQL and PDO

From Dev

PDO insert BindParam mysql

From Dev

MySQL can't find insert?

From Dev

Insert PHP PDO via AJAX

From Dev

Insert PHP PDO via AJAX

From Dev

Can't INSERT INTO my table via _POST

From Dev

PHP PDO can't run query INSERT INTO with SELECT

From Dev

MYSQL Insert Where Not Exists with PDO

From Dev

Using mysql insert with implode in PDO

From Dev

Confirm record INSERT with PDO and MySQL

From Dev

Insert cookies and date pdo mysql

From Dev

How to escape a string to insert into MySQL via PDO *without* using prepared statements (legacy app)

From Dev

Can't insert values to MYSQL table

From Dev

Can't insert string or datetime objects to mysql

From Dev

Can't insert a into DATETIME column in MySQL

From Dev

Can't insert cyrllic symbols into MySQL with JDBC

From Dev

PHP mysql can't insert database and error

From Dev

Can't insert Cyrillic symbols into MySQL with JDBC

From Dev

mysql - can't insert data into database with php

From Dev

mysql can't insert in a table with 8 columns

From Dev

Can't insert russian language into mysql

From Dev

can't insert apostrophe inside textarea and insert to mysql

From Dev

How to tell if PDO MySQL INSERT was successful with IGNORE

From Dev

Insert JSON decoded data into mysql with PDO

From Dev

PDO Insert multiple checkbox values in Mysql

From Dev

PDO Insert operation using PHP and MYSQL not working