doujiao1538 2019-04-05 23:35
浏览 111

查询返回false

Im setting up new function to store a transaction data, the insert query run well, the data was stored in mysql, but my index.php catched a false query, so it didn't fetch the json.

I used localhost as server and postman to test.

below are on my 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);
}

and these are my function

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

here I expected,

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

but it return false like this

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

Im so thankful for your help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用