查询返回false

时间:2019-04-05 23:35:46

标签: php mysql sql

我正在设置新功能来存储事务数据,插入查询运行良好,数据存储在mysql中,但是我的index.php捕获了一个错误查询,因此没有获取json。

我使用localhost作为服务器和邮递员进行测试。

下面是我的index.php

$product_id = $_POST['product_id'];
$user_id = $_POST['user_id'];
$notes = $_POST['notes'];
$times = $_POST['times'];
$status = $_POST['status'];

$transaction = $db->createTransaction($product_id, $user_id, $notes, $times, $status);
if($transaction) {
    $response["error"] = FALSE;
    $response['invoice'] = $transaction['invoice'];
    $response['transaction']['product_id'] = $transaction['product_id'];
    $response['transaction']['user_id'] = $transaction['user_id'];
    $response['transaction']['notes'] = $transaction['notes'];
    $response['transaction']['times'] = $transaction['times'];
    $response['transaction']['status'] = $transaction['status'];
    $response['transaction']['created_at'] = $transaction['created_at'];
    echo json_encode($response);
} else {
    // user failed to store
    // jika gagal didaftarkan
    $response["error"] = TRUE;
    $response["error_msg"] = "Error in creating transaction!";
    echo json_encode($response);
}

这是我的功能

public function createTransaction($product_id, $user_id, $notes, $times, $status) {
    $uid = uniqid('', true);
    $invoice = "INV-".$uid;
    $date  = date("Y-m-d");
    $hour  = date("H");
    // $fix_hour = $hour + 7;  //menyesuaikan zona waktu. jikalau tidak, bisa disetting di mysql
    $time = date("i:s");

    $date = $date." ".$hour.":".$time;
    $stmt = $this->conn->prepare("INSERT INTO transactions(invoice, product_id, user_id, notes, times, status, created_at) VALUES(?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssss", $invoice, $product_id, $user_id, $notes, $times, $status, $date);
    $result = $stmt->execute();
    $stmt->close();
    // check for successful store
    // memriksa apakah berhasil didaftarkan
    if ($result) {
        $stmt = $this->conn->prepare("SELECT invoice FROM transactions WHERE invoice = ?");
        $stmt->bind_param("s", $invoice);
        $stmt->execute();
        $transaction = $stmt->get_result()->fetch_assoc();
        $stmt->close();
        return $transaction; 
    } else {
        return false;
    }
}

我在这里期望

{
    "error": false,
    "invoice": $invoice,
    "transaction": {
        "product_id": $product_id,
        "user_id": $user_id,
        "notes": $notes,
        "times": $times,
        "status": $status,
        "created_at": $created_at
    }
}

但它会返回false

{
    "error": true,
    "error_msg": "Error in creating transaction!"
}

非常感谢您的帮助。

0 个答案:

没有答案