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

Kelly
  • 93
  • 9

1 Answers1

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.

avoliva
  • 3,181
  • 5
  • 23
  • 37
  • Im editing this code from a website and learning as i go...I probably dont need this code then?....for what im doing – Kelly Oct 21 '14 at 03:58
  • 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. – avoliva Oct 21 '14 at 04:00