简体   繁体   English

PHP数据库未添加数据

[英]PHP Database not adding data

I use a try/catch statement in this PHP but my code is executing the catch statement but not sure why, here's the JSON message produced in the catch. 我在此PHP中使用了try / catch语句,但是我的代码正在执行catch语句,但不确定为什么,这是catch中产生的JSON消息。

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

The code is follows below 代码如下

$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));
}

I have looked over the code but nothing is jumping out as incorrect, maybe I am missing something? 我已经查看了代码,但没有什么比不正确的跳出来,也许我缺少了什么?

Insert 插入

$response["error"] = $ex;

in your catch statement so you see what exception happens and triggers the catch. 在catch语句中,以便查看发生了什么异常并触发catch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM