我无法从sql表中获取最后一个order_id

问题描述 投票:-1回答:1

我无法从sql表中获取最后的order_id,我必须递增该订单ID。

<?php

if (isset($_POST['items']) && isset($_POST['quantity']) && 
isset($_POST['room_no'])    ) {

echo "hiii  ----";
$items = $_POST['items'];
$quantity   = $_POST['quantity'];
$room_no    = $_POST['room_no'];


// include db connect class
require_once __DIR__ . '/db_connect.php';

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

$order_id = mysql_query("SELECT order_id  FROM orderitems ORDER BY date_time DESC LIMIT 1");
 $order_id = $order_id + 1;
echo "items are  ----" . $items;
$JSON_Received = $_POST["items"];
$obj = json_decode($JSON_Received, true);



foreach ($obj as $item) {


  $name = $item['name'];
 $count = $item['count'];

 $result = mysql_query("INSERT INTO 
orderitems(order_id,items,quantity,room_no) 
VALUES('$order_id','$name','$count', '$room_no')");
$order_id = mysql_query("SELECT order_id  FROM orderitems ORDER BY date_time 
DESC LIMIT 1");


 // check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Product 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);
        }
      ?>

我无法从sql表中获取最后一个order_id,我必须在执行for循环后递增order_id。我搜索了很多stackoverflow问题,但它没有帮助

非常感谢帮助!!

php mysql sql
1个回答
0
投票
SELECT MAX(order_id ) AS lastestOrder FROM orderitems ;

为什么不在模式中使用自动增量而不是手动执行?

© www.soinside.com 2019 - 2024. All rights reserved.