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

时间:2017-12-19 07:25:31

标签: php mysql sql

我无法从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问题,但它没有帮助

非常感谢帮助!!

1 个答案:

答案 0 :(得分:0)

SELECT MAX(order_id ) AS lastestOrder FROM orderitems ;

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