最后插入的ID返回null

时间:2018-07-31 14:23:26

标签: php

我试图在成功插入后返回上次插入的ID。我已经阅读并搜索了很多线程,但是仍然无法获得所需的信息。在我的返回数组last_inserted_id:null中。我知道我不在这里做某事。

这是我的include / DbOperation.php

class DbConnect
{
    private $conn;

    function __construct()
    {
    }

    /**
     * Establishing database connection
     * @return database connection handler
     */
    function connect()
    {
        require_once 'Constants.php';

        // Connecting to mysql database
        $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Check for database connection error
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        // returing connection resource
        return $this->conn;
    }
}

这是我的方法

   //Function to add delivery address
    public function addDeliveryAddress($recipientName, $recipientPhoneNumber, $recipientArea, $recipientAddress, $nearestLandmark, $userId)
    {
        try{ $stmt = $this->conn->prepare("INSERT INTO deliveryAddressTbl (recipientName, recipientPhoneNumber, recipientArea, recipientAddress, nearestLandmark, userId) VALUES (?, ?, ?, ?, ?, ?)");
            $stmt->bind_param("ssssss", $recipientName, $recipientPhoneNumber, $recipientArea, $recipientAddress, $nearestLandmark, $userId);

            if ($stmt->execute()) {
                //return DELIVERY_ADDRESS_ADDED_SUCCESSFULLY;

                $result = $stmt->insert_id;
                $stmt->close();
                return $result;
            } else {
                return DELIVERY_ADDRESS_COULDNOT_BE_ADDED;
            }
        }
        catch (PDOExeception $e){
            echo $e;
        }

    }

这是我的索引页面

<?php

//importing required script
require_once 'includes/DbOperation.php';

$response = array();
//print array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    //getting values
    $recipientName = $_POST['recipientName'];
    $recipientPhoneNumber = $_POST['recipientPhoneNumber'];
    $recipientArea = $_POST['recipientArea'];
    $recipientAddress = $_POST['recipientAddress'];
    $nearestLandmark = $_POST['nearestLandmark'];
    $userId = $_POST['userId'];

    //creating db operation object
    $db = new DbOperation();

    //adding address to database
    $result = $db->addDeliveryAddress($recipientName, $recipientPhoneNumber, $recipientArea, $recipientAddress, $nearestLandmark, $userId );

    //making the response accordingly
    if ($result != DELIVERY_ADDRESS_COULDNOT_BE_ADDED) {

        $response['error'] = false;
        $response["last_inserted_id"] = $deliveryId;
        $response['message'] = 'Delivery address added successfully';

    } elseif ($result == DELIVERY_ADDRESS_COULDNOT_BE_ADDED) {
        $response['error'] = true;
        $response['message'] = 'Delivery address counldnot be added';
    } 

} else {
    $response['error'] = true;
    $response['message'] = 'Invalid Request!';
}

echo json_encode($response);

谢谢

1 个答案:

答案 0 :(得分:0)

最后一个结果ID保存在$ result中,但您尝试从$ deliveryId获取该值为null且从不设置,请在保存响应时尝试将变量形式从$ deliveryId更改为$ result。

$result = $db->addDeliveryAddress($recipientName, $recipientPhoneNumber, $recipientArea, $recipientAddress, $nearestLandmark, $userId );

        //making the response accordingly
        if ($result != DELIVERY_ADDRESS_COULDNOT_BE_ADDED) {

            $response['error'] = false;
            //$response["last_inserted_id"] = $deliveryId;
              $response["last_inserted_id"] = $result;