尝试捕获不触发功能

I have a class with a method (add_client_to_db), that when called, will add a client to a MySQL database. The function itself works when the values are correct. I want it to write the error to the database (if it receives one). I have used the error logging function before (db_errorHandler) in other projects and it works very well.

In this application I'm currently working on, I'm using AJAX to call the script, and that is the only difference.

When the add_client_to_db function gets good data, it works. If it gets bad data, it won't write the data to the database (which is what I want). It does NOT fire the db_errorHandler function, or it doesn't work for some reason.

Any help would be appreciated. In this example, I am purposely using a bad INSERT command.

functions.js: (This is what calls the add_client.php script)

$('.btn_add_client').on('click', function() {
        var client_type = $('#cb_client_type option:selected').attr('id');
        var first_name = $('#txt_first_name').val();
        var middle_name = $('#txt_middle_name').val();
        var last_name = $('#txt_last_name').val();
        var phone_home = $('#txt_phone_home').val();
        var phone_mobile = $('#txt_phone_mobile').val();
        var phone_office = $('#txt_phone_office').val();
        $.ajax({
            method: 'post',
            url: './scripts/add_client.php',
            data: {
                'client_type': client_type,
                'first_name': first_name,
                'middle_name': middle_name,
                'last_name': last_name,
                'phone_home': phone_home,
                'phone_mobile': phone_mobile,
                'phone_office': phone_office
            },
            success: function(data) {
                $('#form_wrapper').html(data);
            }
        });//end ajax - Add a new client to the database
    });

add_client.php: (This instantiates the Client class and passes in some variables, then attempts to add the client to the database)

require_once '../classes/mysql_login_pdo.php';
require_once '../classes/class.Database.inc';
require_once '../classes/class.client.inc';

$client = new Client($db);

//TODO - Filter POST Variables
$client->clientType = $_POST['client_type'];
$client->firstName = $_POST['first_name'];
$client->middleName = $_POST['middle_name'];
$client->lastName = $_POST['last_name'];
$client->phone_home = $_POST['phone_home'];
$client->phone_mobile = $_POST['phone_mobile'];
$client->phone_office = $_POST['phone_office'];
$client->billing_street_address_1 = "230 N State Rd";
$client->billing_street_address_2 = "Ste 200";
$client->billing_city = "Davison";
$client->billing_state = "MI";
$client->billing_zip = "48423";
$client->customer_status = "0";

$client->add_client_to_db();

class.clients.inc:

...
public function __construct($db) {
        //Get the $db object
        //Must be passed in when instantiating the class
        $this->db = $db;
    }

    public function add_client_to_db() {
        try {
            $this->db->beginTransaction();
            $query = "INSERT INTO `clients`"
                    . " VALUES (:id, :client_type, :firstName, :middleName, :lastName, :phone_home, :phone_mobile, :phone_office,"
                    . " :billing_street_address_1,ddd :billing_street_address_2, :billing_city, :billing_state, :billing_zip, :customer_status, :active)";
            $stmt = $this->db->prepare($query);
            $stmt->execute(array(
                ':id' => null,
                ':client_type' => $this->clientType,
                ':firstName' => $this->firstName,
                ':middleName' => $this->middleName,
                ':lastName' => $this->lastName,
                ':phone_home' => $this->phone_home,
                ':phone_mobile' => $this->phone_mobile,
                ':phone_office' => $this->phone_office,
                ':billing_street_address_1' => $this->billing_street_address_1,
                ':billing_street_address_2' => $this->billing_street_address_2,
                ':billing_city' => $this->billing_city,
                ':billing_state' => $this->billing_state,
                ':billing_zip' => $this->billing_zip,
                ':customer_status' => $this->customer_status,
                ':active' => TRUE
            ));
            $this->db->commit();
        } catch (Exception $e) {
//========================PROBLEMATIC AREA=====================================
            $this->db_errorHandler($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
            $this->db->rollback();
//=============================================================================
        }
    }

...

public function db_errorHandler($errno, $errstr, $errfile, $errline) {
        $query = "INSERT INTO `error_log` "
                . "VALUES(:id,:error_time,:errno,:errstr,:errfile,:errline)";
        $stmt = $this->db->prepare($query);
        $stmt->execute(array(
            ':id' => NULL,
            ':error_time' => NULL,
            ':errno' => $errno,
            ':errstr' => $errstr,
            ':errfile' => $errfile,
            ':errline' => $errline
        ));
        return true; //Don't execute PHP error handler
    }

I think I found the error. I had the following set in my database connection script.

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_NONE);

I changed it to the following, and it seems to work now:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

It seems so simple, but it took a few days to figure out. The errors are logging in the database correctly now. Thanks for trying to help, @Jacob. I appreciate it!