2016-01-31 37 views
0

我發現這個代碼在互聯網上根據我的要求第一次使用EOF改變。我不明白它爲什麼顯示錯誤。 未定義變量:第70行的內容!!!我知道它是未定義的,但我不知道如何解決它。檢查文件是否爲CSV代碼正在與錯誤

<?php 
define('DB_SERVER', 'localhost'); 
define('DB_USER', 'root'); 
define('DB_PASSWORD',""); 
define('DB_NAME', 'uploadcsv'); 

@$conn = mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD); 
mysql_select_db (DB_NAME,$conn); 
if(!$conn){ 
    die("Sorry! There seems to be a problem connecting to our database."); 
} 

function get_file_extension($file_name) { 
    return end(explode('.',$file_name)); 
} 
$error=""; 
function errors($error){ 
    if (!empty($error)) 
    { 
      $i = 0; 
      $showError=""; 
      while ($i < count($error)){ 
      $showError.= '<div class="msg-error">'.$error[$i].'</div>'; 
      $i ++;} 
      return $showError; 
    }// close if empty errors 
} // close function 


if (isset($_POST['upfile'])){ 
// check feilds are not empty 

if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv') 
{ 
$error[] = 'Only CSV files accepted!'; 
} 

if (!$error){ 

$tot = 0; 
$handle = fopen($_FILES["uploaded"]["tmp_name"], "r"); 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    for ($c=0; $c < 1; $c++) { 


      if($data[0] !='keywords'){ 
       mysql_query("INSERT INTO fields(
       keywords, 
       tags 
       )VALUES(
        '".mysql_real_escape_string($data[0])."', 
        '".mysql_real_escape_string($data[1])."' 
       )")or die(mysql_error()); 
      } 

    $tot++;} 
} 
fclose($handle); 
$content.= "<div class='success' id='message'> CSV File Imported, $tot records added </div>"; 

}// end no error 
}//close if isset upfile 
$er = errors($error); 
$content.= <<<EOF 
<h3>Import CSV Data</h3> 
$er 
<form enctype="multipart/form-data" action="" method="post"> 
    File:<input name="uploaded" type="file" maxlength="20" /><input type="submit" name="upfile" value="Upload File"> 
</form> 
EOF; 
echo $content; 
?> 
+0

不要使用'mysql_ *'它已被刪除,將不再工作。使用'PDO' [link](http://php.net/manual/en/book.pdo.php)或'MySQLi' [link](http://php.net/manual/en/book.mysqli。而不是。 – Tom

+0

好吧,讓我看看.. –

+0

沒有,甚至沒有工作 –

回答

0

這是因爲您沒有創建變量。 $content的第一個用法是向它添加字符串部分。

因此,在行59

$content.= "<div class='success' id='message'> CSV File Imported, $tot records added </div>"; 

=之前卸下.應變量的工作,因爲你創造它,而不是試圖來連接它。

$content= "<div class='success' id='message'> CSV File Imported, $tot records added </div>"; 
+0

我以前做過,它不工作 –

+0

嘿其實它的部分工作..我改變了美元的內容。與$內容無論它在代碼和代碼工作正常。但之前如果我添加了15個字段,我以前添加了15個字段,現在沒有顯示任何消息 –

+0

'$ content ='更改字符串值,'$ content。='將爲實際字符串添加更多文本。 – Phiter