Generate invoice number from 0 to so on for ordered products in orders table in php mysqli I am trying to generate invoice number in orders table, like if user select 1 or multiple products and add it to cart then place order then invoice number should be same in our orders table for that 1 order in multiple rows according to products.
I was using function mt_rand(); but it generates randomly. I want to generate numbers from 0 to so on.
Like, 1, 2, 3, 4, 5, 6, 7 and so on.
I've also tried this to generate invoice number.
$i++

$squery = "SELECT * FROM cart WHERE user_id = '$user_id'";
$sresult = $this-&gt;db-&gt;select($squery);
if($sresult){
    $totalPrice = 0;
    $i = 0;
    while($svalue = $sresult-&gt;fetch_assoc()){
        $i++;
        $pro_id = $svalue['pro_id'];
        $pro_name = $svalue['pro_name'];
        $quantity = $svalue['quantity'];
        $pro_price = $svalue['pro_price'] * $quantity;
        $pro_image = $svalue['pro_image'];
        $query = "INSERT INTO orders(user_id, pro_id, invoice_no, pro_name, status, date) "
                . "VALUES('$user_id', '$pro_id', '$i', '$pro_name', 'processing', NOW())";
        $result = $this-&gt;db-&gt;insert($query);
        if($result){
            header("location: orders-list.php");
        } else {
            $msg = "&lt;div class='alert alert-danger'&gt;Something's went wrong. Please try again.&lt;/div&gt;";
            return $msg;
        }
    }
    return true;
}


But it doesn't inserts same number in multiple ordered products rows of 1 order.
Please help me how can i generate invoice number from 0 to so on.
Okay, As per our discussion, what you want is
the same invoice number to, one order(one order can have multiple products but from one User).

So I think you need one table which can save the Invoice Number So when you are inserting into order table before that you fetch the Invoice Number and increment it, then you in order table

$INV_NUM = "SELECT INVOICE_NUMBER FROM INVOICE";
$INV_NUM++;

$squery = "SELECT * FROM cart WHERE user_id = '$user_id'";
$sresult = $this-&gt;db-&gt;select($squery);
if($sresult){
    $totalPrice = 0;
    $i = 0;
    while($svalue = $sresult-&gt;fetch_assoc()){
....................
....................
....................
 $query = "INSERT INTO orders(user_id, pro_id, invoice_no, pro_name, status, date) "
                . "VALUES('$user_id', '$pro_id', '$INV_NUM', '$pro_name', 'processing', NOW())";
....................
....................


This strategy might help you. Please let me know, it works or not?