Home » Php » php – trying to select max invoice id from database

php – trying to select max invoice id from database

Posted by: admin February 25, 2020 Leave a comment

Questions:

hello i am trying to select max invoice id from a table to get different invoice id everytime customer buy products so i tried this query but i always get 0 in the invoice id

 if (isset($_POST['orderr'])) {

$user = mysqli_query($conn," SELECT * FROM user_account WHERE id='$id'");
$fetch_ueser=mysqli_fetch_assoc($user);
$fullname=$fetch_ueser['fullname'];
$phone=$fetch_ueser['phone'];
$location=$fetch_ueser['location'];

$Inv = mysqli_query($conn,"SELECT(isnull((SELECT max (invoice_id) as invoice_id FROM sales),0))+1");
$fetch_inv=mysqli_fetch_assoc($inv);
$invoice=$fetch_inv['invoice_id'];



$get = mysqli_query($conn," SELECT * FROM cart WHERE u_id='$id' ");

while ($row=mysqli_fetch_assoc($get)) {


    $price = $row['price'];
    $img = $row['img'];
    $name = $row['name'];
    $qty = $row['qty'];
    $date=$row['date'];
    $p_id=$row['p_id'];
    $total=$qty*$price;
    $p_id=$row['p_id'];
    $img = $row['img'];


        $insert = mysqli_query($conn, "INSERT INTO sales(customer_name,phone,location,product_name,product_id,price,quantity,sell_date,total,img,invoice_id) VALUES('$fullname','$phone','$location','$name','$p_id','$price','$qty','$date','$total','$img','$invoice')");

        if ($insert) 
        {

        $delete1 = mysqli_query($conn, "DELETE FROM cart WHERE p_id='$p_id' AND u_id='$id'");
          header("location:cart.php");
        }
    }

}

other data are inserted well but the invoice id is always 0 can any one help me

How to&Answers: