簡體   English   中英

使用php插入值后,如何獲取行的自動遞增ID?

[英]How to get the auto-incremented ID for a row once you insert values using php?

我有一個MySQL數據庫,正在使用httpRequest將其與我的Android應用程序連接。 我有一個php文件,該文件將通過httpPost和它要插入的表插入值,以具有自動遞增的ID來保持每一行的唯一性。

我想做的是能夠通過php插入值並獲得插入行的ID,然后將其用於另一個插入查詢。 任何想法如何通過PHP做到這一點?

這是我當前的代碼:

<?php

/*
 * Following code will insert an order request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['table_ID']) && isset($_POST['MenuBook']) && isset($_POST['order_status'])&& isset($_POST['order_date'])&& isset($_POST['order_receipt'])) {

    $tableID = $_POST['table_ID'];
    $menuBook = $_POST['MenuBook'];
    $order_status = $_POST['order_status'];
    $order_date = $_POST['order_date'];
    $order_receipt = $_POST['order_receipt'];

    // inlude db connect class
    require_once __DIR__ . '/DBConnect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO orderdb(table_ID,MenuBook,order_status,order_date,order_receipt) VALUES('$table_ID', '$menuBook', '$order_status','$order_date','$order_receipt')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Order successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

插入查詢后,使用mysql-insert-id

<?php 
...
$result = mysql_query("INSERT INTO orderdb(table_ID,MenuBook,order_status,order_date,order_receipt) VALUES('$table_ID', '$menuBook', '$order_status','$order_date','$order_receipt')");
$last_ID = mysql_insert_id();
...
?>

從mysql_insert_id()的文檔中:

成功執行前一個查詢為AUTO_INCREMENT列生成的ID;如果前一個查詢未生成AUTO_INCREMENT值,則為0;如果未建立MySQL連接,則為FALSE。

您可以調用一個函數,該函數將在自動增量列上返回上一個查詢的ID。 通常,只要您插入或更新並需要ID,就可以調用此方法。

MySQL:$ id = mysql_insert_id(); //返回最后插入的ID

http://php.net/manual/zh/function.mysql-insert-id.php

MySQLi的:$ id = $ mysqli-> insert_id

http://www.php.net/manual/zh/mysqli.insert-id.php

查詢后,您可以使用mysql_insert_id() http://php.net/manual/zh/function.mysql-insert-id.php

但是您使用的mysql驅動程序已過時,因此也許您應該使用mysqli或pdo。

只需使用如下所示的mysql_insert_id ,盡管您確實應該轉換為mysqliPDO ,這兩者具有相似的功能。

// mysql inserting a new row
$result = mysql_query("INSERT INTO orderdb(table_ID,MenuBook,order_status,order_date,order_receipt) VALUES('$table_ID', '$menuBook', '$order_status','$order_date','$order_receipt')");

// check if row inserted or not
if ($result) {
    $insert_id = mysql_insert_id($db);

    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Order successfully created.";

    // echoing JSON response
    echo json_encode($response);
} else {
used_id = mysql_insert_id($db);

請考慮遷移到mysqli模塊,mysql_query是危險的,並為sql注入打開了大門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM