我正在尝试通过PHP将数据从CSV文件导入MySql。
我有2个csv文件。一个看起来如下:
http://i49.tinypic.com/2uo30gp.png
我已成功将其插入我的数据库。我使用了以下代码:
<?php
require("config.php");
mysql_select_db("magic",$db_link); //select the table
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO alvis_input (a_zone, a_address) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."'
)
");
}
} while ($data = fgetcsv($handle,10000,",","'"));
//
//redirect
header('Location: file_alvis.php?success=1'); die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Import data from CSV file</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
现在我需要从一个新的csv文件在同一个mysql表中插入数据,如下所示:
http://i48.tinypic.com/2vv57xj.png
你可以看到,新的csv只有1列包含'zone 175/4'这样的数据我需要删除'zone'并拆分'175'和'4'然后需要插入带有2列的数据库表
知道如何处理这个吗?