I am attempting to insert data into a database, but I keep getting a undefined offset error and I can not figure out why. The error log says the error is on line 41 which is
if(trim($_POST["item"][$i] !='')){
the program originally wrote to a text file and all the code worked the only new lines are these:
$data = explode(",", ${'item'.$i});
$query = "INSERT INTO batching (ing_date, ing_id, allergen, lot, batch_date, batch_id, batch_num)
VALUES($data[0],$data[1],$data[3],$data[4],$date,$rft_batch,1)";
if(isset($_POST['submit'])){
        $rft_batch = $_POST['rft_batch'];
        $date = $_POST['date'];
        $number = count($_POST);
        echo ("<h2>Batch Number: " . $rft_batch . " Batching Date: " . $date . "</h2><br />");
        if($number > 1) {
            for($i=0; $i<$number; $i++){
                if(trim($_POST["item"][$i] !='')){
                    ${'item'.$i} = $_POST["item"][$i];
                    $data = explode(",", ${'item'.$i});
                    $query = "INSERT INTO batching (ing_date, ing_id, allergen, lot, batch_date, batch_id, batch_num)
                    VALUES ($data[0],$data[1],$data[3],$data[4],$date,$rft_batch,1)";
                    echo (${'item'.$i}."<br />");
                }
            }
        }
        if (!mysqli_query($conn, $query)){
            die('An error occured. YOU SUCK AT CODING!');
        } else {
            echo ("GOOD JOB YOU FILTHY ANIMAL");
        }
    }
 
     
    