我对php和sql非常生疏,我在使用以下代码时遇到了一些问题。我收到错误:
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
这些错误会持续几行,但你明白了 - 基本上我的CSV文件中的每个索引#。
我已经关注了一些教程,并在这里寻找解决方案,但没有其他人似乎有确切的问题。它不是CSV文件 - 它使用标准逗号分隔值,我确定那里有数据,因为当我运行顶部时,它会打印出我的整个csv文件,标题和所有内容。另一件事是如何让它不将csv的标题插入我的数据库?很抱歉这样的菜鸟。正如我所提到的,我在几年内没有做过这样的任何编码。
请参阅下面的完整代码:
<?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
?>
全部谢谢:)希望我能解决这个问题!