0

尝试使用 php 命令 fcsvget 将 csv 文件中的一些数据插入到 mysql 表中,但没有人能告诉我我哪里出错了。我在加载页面时没有数据进入和错误。

csv 文件具有不同的标题名称和要进入的表的列数。

到目前为止,这是我的代码。

<?php

// set local variables
$connect = mysql_connect("localhost","db","password") or die('Could not connect: ' . mysql_error());
$handle = fopen("test.csv", "r");

// connect to mysql and select database or exit 
mysql_select_db("db", $connect);

// loop content of csv file, using comma as delimiter

while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$Product_ID = $data[0];
$Model = $data[1];

// entry insert
$query = "INSERT into products_test  (products_id, products_model) VALUES '$Product_ID', $Model";


mysql_query($query);
if (mysql_affected_rows() <= 0) {

// no rows where affected by update query
}
} else {
// entry doesn't exist continue or insert...
}

mysql_free_result($result);
}

fclose($handle);
mysql_close($connect);

?>
4

2 回答 2

0
while (($data = fgetcsv($handle, 10000, "|")) !== false) {
$Product_ID =  $data[0];
$Model = $data[1];
$HighPic = $data[2];
$ManuId = $data[3];
$parent_id = $data[7];
$Image = $data[13];
$Price = $data[14];
$Supplier = $data[15];
$Stock = $data[17];



// entry insert
$query = "INSERT into products_test (products_id, products_model, products_image, manufacturers_id, products_parent_id, icecat_manfimg, products_price, icecatcode, products_quantity) VALUES ($Product_ID, $Model, $HighPic, $ManuId, $parent_id, $Image, $Price, $Supplier, $Stock)" ;

mysql_query($query);
} 

mysql_free_result($result);


fclose($handle);
mysql_close($connect);

?>
于 2013-05-02T16:58:57.230 回答
0

检查你的代码..

    } else {
     // entry doesn't exist continue or insert...
    }

没有效果,因为没有为此启动 IF。唯一的那个

   if (mysql_affected_rows() <= 0) {

    // no rows where affected by update query
  }

在 'Else' 语句之前已经关闭。我无法进入您的查询,但这是我第一眼看到的

于 2013-05-02T13:57:43.423 回答