尝试Catch not firing功能

时间:2014-08-10 01:07:14

标签: php jquery mysql ajax

我有一个带有方法(add_client_to_db)的类,调用时会将客户端添加到MySQL数据库中。当值正确时,函数本身可以工作。我希望它将错误写入数据库(如果它收到一个)。我在其他项目中使用了错误日志记录功能(db_errorHandler),它运行得很好。

在我正在处理的这个应用程序中,我正在使用AJAX来调用脚本,这是唯一的区别。

当add_client_to_db函数获得良好的数据时,它可以工作。如果它获得了错误的数据,它将不会将数据写入数据库(这就是我想要的)。它不会触发db_errorHandler函数,或者由于某种原因它不起作用。

任何帮助将不胜感激。在这个例子中,我故意使用错误的INSERT命令。

functions.js :(这是调用add_client.php脚本的内容)

$('.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 :(这会实例化Client类并传入一些变量,然后尝试将客户端添加到数据库中)

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
    }

1 个答案:

答案 0 :(得分:0)

我想我发现了错误。我在数据库连接脚本中设置了以下内容。

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

我将其更改为以下内容,现在似乎正常工作:

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

看起来很简单,但花了几天才弄明白。错误现在正确登录数据库。感谢您试图帮助,@ Jacob。我很感激!