0

Could some one help me out for some reason im getting an error and can not figure out why!!!! I have about 45 columns and im only needing to use 1,2,3,4,7,8 from the table so i cant load everything.

    <? 
        $con=mysqli_connect("localhost","*******","*****","******");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
        //get the csv file
    $file = "tickets/feed/rtc.csv";
    $handle = fopen($file,"r");
    $count=0;
    $sid="32837459823";
    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysqli_query($con,"INSERT INTO carlist (id, stknum, vin, vt, stat, other, store_id) VALUES
                (
                    '".$count++."',
                    '".addslashes($data[7])."',
                    '".addslashes($data[8])."',
                    '".addslashes($data[2].$data[3].$data[4])."',
                    '".addslashes($data[1])."',
                    '".addslashes("0")."',
                    '".addslashes($sid)."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    //
        ?>

Here is the errors im getting

Notice: Undefined variable: data in /home1/inventory/mobile/actions/loadsubmit.php on line 139

Notice: Undefined offset: 7 in /home1/inventory/mobile/actions/loadsubmit.php on line 139

Notice: Undefined offset: 8 in /home1/inventory/mobile/actions/loadsubmit.php on line 140

Notice: Undefined offset: 2 in /home1/inventory/mobile/actions/loadsubmit.php on line 141

Notice: Undefined offset: 3 in /home1/inventory/mobile/actions/loadsubmit.php on line 141

Notice: Undefined offset: 4 in /home1/inventory/mobile/actions/loadsubmit.php on line 141

Notice: Undefined offset: 1 in /home1/inventory/mobile/actions/loadsubmit.php on line 142

1

1 Answer 1

1

Looks like $data is not defined until you hit

while ($data = fgetcsv($handle,1000,",","'"));

so when you're trying to access $data before that line you're getting an error because the variable doesn't exist.

Sign up to request clarification or add additional context in comments.

2 Comments

Im editing this code from a website and learning as i go...I probably dont need this code then?....for what im doing
Just make it a while loop instead of a do-while loop. Put the condition at the top of the loop, that will probably help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.