PHP数据库不添加数据

时间:2015-11-16 13:22:51

标签: php mysql json database mysqli

我在这个PHP中使用了try / catch语句但是我的代码正在执行catch语句但不确定原因,这里是catch中生成的JSON消息。

{"success":0,"message":"Database Error1. Please Try Again!"}

代码如下:

$query        = " SELECT 1 FROM tbl_client WHERE master_username = :user";
$query2        = " SELECT 1 FROM tbl_client WHERE institution_pin = :institution_pin";


$query_params = array(
    ':user' => $_POST['username']
);

$query_params2 = array(
    ':institution_pin' => $_POST['institution_pin']
);

try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {

    $response["success"] = 0;
    $response["message"] = "Database Error1. Please Try Again!";
    die(json_encode($response));
}

$row = $stmt->fetch();
if ($row) {

    $response["success"] = 0;
    $response["message"] = "I'm sorry, this username is already in use";
    die(json_encode($response));
}
try { 
$stmt   = $db->prepare($query2);
$result = $stmt->execute($query_params2);
}
catch (PDOException $ex) {

    $response["success"] = 0;
    $response["message"] = "Database Error1. Please Try Again!";
    die(json_encode($response));
}
$row = $stmt->fetch();
if ($row) {
    $response["success"] = 0;
    $response["message"] = "I'm sorry, someone has already chosen that PIN, choose another!";
    die(json_encode($response));
}

我查看了代码,但没有任何内容错误地跳出来,也许我错过了什么?

1 个答案:

答案 0 :(得分:0)

插入

$response["error"] = $ex;
在你的catch语句中

,这样你就可以看到发生了什么异常并触发了catch。