0

我有一个 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);
}
?>
4

5 回答 5

2

在插入查询之后,使用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。

于 2013-02-20T21:57:53.020 回答
2

您可以调用一个函数,该函数将在自动增量列上返回上一个查询的 id。通常,每当您插入或更新并需要 ID 时,您都会调用它。

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

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

MySQLi: $id = $mysqli->insert_id

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

于 2013-02-20T22:07:20.303 回答
1

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

但是你使用的mysql驱动已经过时了,所以也许你应该使用mysqli或pdo。

于 2013-02-20T21:58:21.893 回答
1

只需mysql_insert_id如下所示使用,尽管您确实应该转换为mysqlior PDO,它们都具有相似的功能。

// 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 {
于 2013-02-20T22:00:38.740 回答
1
used_id = mysql_insert_id($db);

请考虑迁移到 mysqli 模块,mysql_query 很危险,并为 sql 注入打开了大门。

于 2013-02-20T22:00:41.947 回答