Php 我无法从sql表中获取上一个订单id

Php 我无法从sql表中获取上一个订单id,php,mysql,sql,Php,Mysql,Sql,我无法从sql表中获取最后一个订单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[

我无法从sql表中获取最后一个订单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);
        }
      ?>


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

为什么没有
订单id
作为
自动递增
字段?为什么还要继续使用弃用的
mysql*
函数系列,而不是
mysqli
PDO
——此代码容易受到sql注入的攻击<代码>$order\u id=mysql\u查询(“从orderitems中选择订单id order BY date\u time DESC LIMIT 1”)
显然是错误的-在尝试访问记录之前,您需要
获取结果,我不想在每次插入后递增。只有我必须在执行for循环后递增。您的问题没有明确说明这一点。请停止使用PHP的不推荐的mysql_uuAPI。我投票将此问题作为主题外的问题结束,因为它引用了一个过时、不推荐且不安全的API。
SELECT MAX(order_id ) AS lastestOrder FROM orderitems ;