I'm rather rusty with php and sql and i'm having some issues getting the following code to work. I get the error:
Notice: Undefined offset: 1 in C:\xampp\htdocs\SAMPi\index.php on line 38
Notice: Undefined offset: 2 in C:\xampp\htdocs\SAMPi\index.php on line 39
Notice: Undefined offset: 3 in C:\xampp\htdocs\SAMPi\index.php on line 40
Notice: Undefined offset: 4 in C:\xampp\htdocs\SAMPi\index.php on line 41
Notice: Undefined offset: 5 in C:\xampp\htdocs\SAMPi\index.php on line 42
Notice: Undefined offset: 6 in C:\xampp\htdocs\SAMPi\index.php on line 43
Notice: Undefined offset: 7 in C:\xampp\htdocs\SAMPi\index.php on line 44
Notice: Undefined offset: 8 in C:\xampp\htdocs\SAMPi\index.php on line 45
These errors go on for a few more lines but you get the idea - basically every index # in my CSV file.
I've followed a few tutorials and looked on here for solutions but no one else seems to have the exact issue. It's not the CSV file - it uses standard commas to separate values and I'm sure there is data there because when I run the top part it does a print out of my entire csv file, headers and all. Another thing is how can I get it to not insert the headers of the csv into my database? Sorry to be such a noob. As I mentioned I haven't really done any coding like this in a few years.
Please see my full code below:
<?php
$file = fopen("data/20151028_22.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
echo "<br><br><br> Hopefully this works! <br><br><br>";
// specify connection info
$connect = mysql_connect('localhost','root','');
if (!$connect)
{
die('Could not <span id="IL_AD1" class="IL_AD">
connect to</span> MySQL: ' . mysql_error());
}
$date = date("ymd");
$cid =mysql_select_db('sampi',$connect); //specify db name
define('CSV_PATH','C:/xampp/htdocs/SAMPi/data/'); // specify CSV file path
$csv_file = CSV_PATH . "20151028_22.csv"; // Name of your CSV file - Need to tweak this for date+shop
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['Hours'] = $csv_array[0];
$insert_csv['Total_Takings'] = $csv_array[1];
$insert_csv['Cash'] = $csv_array[2];
$insert_csv['Credit_Card'] = $csv_array[3];
$insert_csv['PLU_1'] = $csv_array[4];
$insert_csv['PLU_2'] = $csv_array[5];
$insert_csv['PLU_3'] = $csv_array[6];
$insert_csv['PLU_4'] = $csv_array[7];
$insert_csv['PLU_5'] = $csv_array[8];
$insert_csv['PLU_6'] = $csv_array[9];
$insert_csv['PLU_7'] = $csv_array[10];
$insert_csv['PLU_8'] = $csv_array[11];
$insert_csv['PLU_9'] = $csv_array[12];
$insert_csv['PLU_10'] = $csv_array[13];
$insert_csv['Customer_Count'] = $csv_array[14];
$insert_csv['First_Transaction'] = $csv_array[15];
$insert_csv['Last_Transaction'] = $csv_array[16];
$insert_csv['No_Sale'] = $csv_array[17];
$query = "INSERT INTO csvdata(ID,Date,Hours,Total_Takings,Cash,Credit_Card,PLU_1,PLU_2,PLU_3,PLU_4,PLU_5,PLU_6,PLU_7,PLU_8,PLU_9,PLU_10,Customer_Count,First_Transaction,Last_Transaction,No_Sale)
VALUES('',".$date.",'".$insert_csv['Hours']."','".$insert_csv['Total_Takings']."','".$insert_csv['Cash']."','".$insert_csv['Credit_Card']."','".$insert_csv['PLU_1']."','".$insert_csv['PLU_2']."','".$insert_csv['PLU_3']."','".$insert_csv['PLU_4']."','".$insert_csv['PLU_5']."','".$insert_csv['PLU_6']."','".$insert_csv['PLU_7']."','".$insert_csv['PLU_8']."','".$insert_csv['PLU_9']."','".$insert_csv['PLU_10']."','".$insert_csv['Customer_Count']."','".$insert_csv['Customer_Count']."','".$insert_csv['First_Transaction']."','".$insert_csv['Last_Transaction']."','".$insert_csv['No_Sale']."')";
$n=mysql_query($query, $connect );
$i++;
}
fclose($csvfile);
echo "File data successfully imported to database!!";
mysql_close($connect); // closing connection
?>
Thanks All :) hopefully I can work this out!