第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

PHP導(dǎo)入CSV未定義的偏移量

PHP導(dǎo)入CSV未定義的偏移量

PHP
子衿沉夜 2023-08-19 14:15:38
我想將 CSV 文件上傳到我的 SQL 數(shù)據(jù)庫(kù)中,但收到&ldquo;未定義的偏移量&rdquo;錯(cuò)誤。(偏移量:1-20)。不知道如何解決該錯(cuò)誤。我希望你們能為我指出正確的方向。提前致謝。<?php// Load the database configuration fileinclude_once 'dbConfig.php';if(isset($_POST['importSubmit'])){? ??? ? // Allowed mime types? ? $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');? ??? ? // Validate whether selected file is a CSV file? ? if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $csvMimes)){? ? ? ??? ? ? ? // If the file is uploaded? ? ? ? if(is_uploaded_file($_FILES['file']['tmp_name'])){? ? ? ? ? ??? ? ? ? ? ? // Open uploaded CSV file with read-only mode? ? ? ? ? ? $csvFile = fopen($_FILES['file']['tmp_name'], 'r');? ? ? ? ? ??? ? ? ? ? ? // Skip the first line? ? ? ? ? ? fgetcsv($csvFile);? ? ? ? ? ??? ? ? ? ? ? // Parse data from CSV file line by line? ? ? ? ? ? while(($line = fgetcsv($csvFile)) !== FALSE){? ? ? ? ? ? ? ? // Get row data? ? ? ? ? ? ? ? $postcode? ?= $line[0];? ? ? ? ? ? ? ? $week1? = $line[1];? ? ? ? ? ? ? ? $week2? = $line[2];? ? ? ? ? ? ? ? $week3? = $line[3];? ? ? ? ? ? ? ? $week4? = $line[4];? ? ? ? ? ? ? ? $week5? = $line[5];? ? ? ? ? ? ? ? $week6? = $line[6];? ? ? ? ? ? ? ? $week7? = $line[7];? ? ? ? ? ? ? ? $week8? = $line[8];? ? ? ? ? ? ? ? $week9? = $line[19];? ? ? ? ? ? ? ? $week10? = $line[10];? ? ? ? ? ? ? ? $week11? = $line[11];? ? ? ? ? ? ? ? $week12? = $line[12];? ? ? ? ? ? ? ? $week13? = $line[13];? ? ? ? ? ? ? ? $week14? = $line[14];? ? ? ? ? ? ? ? $week15? = $line[15];? ? ? ? ? ? ? ? $week16? = $line[16];? ? ? ? ? ? ? ? $week17? = $line[17];? ? ? ? ? ? ? ? $week18? = $line[18];? ? ? ? ? ? ? ? $week19? = $line[19];? ? ? ? ? ? ? ? $week20? = $line[20];? ? ? ? ? ? }? ? ? ? }錯(cuò)誤信息:
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

聽(tīng)起來(lái)輸入文件有一些不太合適的行。


您可能需要在插入之前進(jìn)行一些檢查,例如


$line_no = 1;


// Parse data from CSV file line by line

while(($line = fgetcsv($csvFile)) !== FALSE){

? ? $line_no++;

? ? if ( count($line) != 21 ) {??

? ? ? ? echo "$line_no needs to be looked at";

? ? ? ? continue;? ? ? ?// go to next iteration of the loop

? ? }

? ? // Get row data

? ? $postcode? ?= $line[0];

? ? $week1? = $line[1];

? ? $week2? = $line[2];

? ? $week3? = $line[3];

? ? $week4? = $line[4];

? ? $week5? = $line[5];

? ? $week6? = $line[6];

? ? $week7? = $line[7];

? ? $week8? = $line[8];

? ? $week9? = $line[19];

? ? $week10? = $line[10];

? ? $week11? = $line[11];

? ? $week12? = $line[12];

? ? $week13? = $line[13];

? ? $week14? = $line[14];

? ? $week15? = $line[15];

? ? $week16? = $line[16];

? ? $week17? = $line[17];

? ? $week18? = $line[18];

? ? $week19? = $line[19];

? ? $week20? = $line[20];

? ??

? ? $db->query("INSERT INTO nlbelevering?

? ? ? ? ? ? (Postcode, Week1, Week2, Week3, Week4, Week5, Week6,?

? ? ? ? ? ? Week7, Week8, Week9, Week10, Week11, Week12, Week13,?

? ? ? ? ? ? Week14, Week15, Week16, Week17, Week18, Week19,?

? ? ? ? ? ? Week20)?

? ? VALUES ('".$postcode."', '".$week1."', '".$week2."',?

? ? ? ? ? ? '".$week3."', '".$week4."', '".$week5."',?

? ? ? ? ? ? '".$week6."', '".$week7."', '".$week8."',?

? ? ? ? ? ? '".$week9."', '".$week10."', '".$week11."',?

? ? ? ? ? ? '".$week12."', '".$week13."', '".$week14."',?

? ? ? ? ? ? '".$week15."', '".$week16."', '".$week17."',?

? ? ? ? ? ? '".$week18."', '".$week19."', '".$week20."'");

? ? }

}

當(dāng)然,您可以考慮在文件預(yù)解析中檢查所有行,如果任何行少于 21 列,則拒絕整個(gè)文件。這一切都取決于數(shù)據(jù),以及數(shù)據(jù)庫(kù)中缺少幾行是否有意義。

重要提示?您的腳本容易受到SQL 注入攻擊。即使您轉(zhuǎn)義輸入,也不安全!您應(yīng)該考慮在或API 中?使用準(zhǔn)備好的參數(shù)化語(yǔ)句,而不是連接值MYSQLI_PDO

準(zhǔn)備好的語(yǔ)句不僅可以避免 SQL 注入問(wèn)題,還可以使用準(zhǔn)備好的語(yǔ)句加快處理速度,因?yàn)槟恍铚?zhǔn)備(發(fā)送到數(shù)據(jù)庫(kù)、編譯和優(yōu)化)查詢一次,而不是每行輸入準(zhǔn)備一次。

// prepare once outside the loop

$stmt = $db->prepare(

? ? ? ? ? ? "INSERT INTO nlbelevering?

? ? ? ? ? ? ? ? ? ? (Postcode, Week1, Week2, Week3, Week4, Week5, Week6,?

? ? ? ? ? ? ? ? ? ? Week7, Week8, Week9, Week10, Week11, Week12, Week13,?

? ? ? ? ? ? ? ? ? ? Week14, Week15, Week16, Week17, Week18, Week19, Week20)?

? ? ? ? ? ? VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

? ? ? ? ? ? );


while(($line = fgetcsv($csvFile)) !== FALSE){


? ? // bind new values each time round the loop and execute the query

? ? $stmt->bind_param('sssssssssssssssssssss',

? ? ? ? ? ? ? ? ? ? $line[0], $line[1], $line[2], $line[3],

? ? ? ? ? ? ? ? ? ? $line[4], $line[5], $line[6], $line[7],

? ? ? ? ? ? ? ? ? ? $line[8], $line[19], $line[10], $line[11],

? ? ? ? ? ? ? ? ? ? $line[12], $line[13], $line[14], $line[15],

? ? ? ? ? ? ? ? ? ? $line[16], $line[17], $line[18], $line[19],line[20]

? ? ? ? ? ? ? ? );

? ? $stmt->execute();

}

看到 CSV 文件后更新。


CSV 代表“逗號(hào)分隔值”,因此fgetcsv()假設(shè)文件如下


aaa,bbb,ccc,ddd,.......

如果您有一個(gè)“分號(hào)分隔值”文件,您必須告訴您fgetcsv()期望有一個(gè)不同的選擇器,因此從看到您的文件更改您的代碼以使用


// parmeter 2 need to be there to use param 3,?

// pick a number larger than any of the line, lenght or Newline will?

// denote a line.


// parameter 3 says to expect `;` as the seperator instead of a `,`


fgetcsv($csvFile, 1000, ";");


查看完整回答
反對(duì) 回復(fù) 2023-08-19
?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

當(dāng)通過(guò) php 中的索引訪問(wèn)數(shù)組元素時(shí),您應(yīng)該始終檢查該索引是否確實(shí)存在。否則你會(huì)收到Undefined offset通知。你應(yīng)該對(duì)每個(gè)元素都這樣做:


$postcode = isset($line[0]) ? strval($line[0]) : '';

$week1 = isset($line[1]) ? strval($line[1]) : '';

...

這樣,如果鍵不存在,您還可以定義默認(rèn)值(在我的示例中它是空字符串“”)。


查看完整回答
反對(duì) 回復(fù) 2023-08-19
  • 2 回答
  • 0 關(guān)注
  • 172 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)