Php 使用唯一值将.tsv文件导入mysql
用户只能上载.tsv文件。我想以唯一值将此文件导入mysql。不允许重复。如何执行此操作。我尝试了此代码,但在插入数据时出错Php 使用唯一值将.tsv文件导入mysql,php,mysql,database,import,tsv,Php,Mysql,Database,Import,Tsv,用户只能上载.tsv文件。我想以唯一值将此文件导入mysql。不允许重复。如何执行此操作。我尝试了此代码,但在插入数据时出错 <?php include_once("class/dbo.class.php"); $message = null; $allowed_extensions = array('tsv'); $upload_path = 'uploads'; if (!empty($_FILES['file'])) { if ($_FILES['file'
<?php
include_once("class/dbo.class.php");
$message = null;
$allowed_extensions = array('tsv');
$upload_path = 'uploads';
if (!empty($_FILES['file'])) {
if ($_FILES['file']['error'] == 0) {
// check extension
$file = explode(".", $_FILES['file']['name']);
$extension = array_pop($file);
if (in_array($extension, $allowed_extensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name']))
{
$filename = $upload_path.'/'.$_FILES['file']['name'];
$content = file_get_contents($filename);
$lines = explode("\n", $content);
$columns = explode("\t", $lines[0]);
//print_r($columns);
$sql_insert = "\n\nINSERT INTO tsv_table (";
foreach ($columns as $column)
{
if($column == end($columns))
$sql_insert .= "'".addslashes($column)."')";
else
$sql_insert .= "'".addslashes($column)."', ";
}
$sql_insert .= " VALUES\n";
$total_lines = count($lines) -1;
for($i=1;$i<$total_lines;$i++)
{
$fields = explode("\t", $lines[$i]);
//print_r($fields);
//exit;
$sql_insert .= "(";
foreach ($fields as $field)
{
if($field == end($fields))
$sql_insert .= "'".addslashes($field)."'";
//else if($field == null)
//$sql_insert .= "'',";
else
$sql_insert .= "'".addslashes($field)."', ";
}
if(($i+1) == $total_lines)
$sql_insert .= ");";
else
$sql_insert .= "),\n";
}
$d=new dbo();
dbo.$d->dml($sql_insert);
echo "Data imported successfully.";
}
} else {
$message = '<span class="red">Only .tsv file format is allowed</span>';
}
} else {
$message = '<span class="red">There was a problem with your file</span>';
}
}
?>
<html>
<head>
<title>Upload TSV to MySQL</title>
<link href="css/core.css" rel="stylesheet" type="text/css" />
</head>
<body>
<section id="wrapper">
<form action="" method="post" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" border="0" class="table">
<tr>
<th><label for="file">Select file</label> <?php echo $message; ?></th>
</tr>
<tr>
<td><input type="file" name="file" id="file" size="30" /></td>
</tr>
<tr>
<td><input type="submit" id="btn" class="fl_l" value="Submit" /></td>
</tr>
</table>
</form>
</section>
</body>
</html>
不要引用表列
试一试
仍然不起作用:列计数与第1行的值计数不匹配,它打印如下插入查询:插入tsv_表(ID、上次扫描日期、名称、怪物等级、力量等级、训练师、近战攻击、粉碎、劈砍、穿透、火力、冷却、电能、风、神圣、黑暗、灵魂、无效、受影响的减伤)值('1','2013-05-04T18:34:43.136Z','Green Slime','COMMON','Cruching','AVERAGE','AVERAGE','AVERAGE','AVERAGE','AVERAGE','AVERAGE','AVERAGE','Slime','Slime','AVERAGE',',('2','2013-05-04T18:35:41.138Z','Attracle Monster','COMMON','Spiring',…我注意到POWER_级别和TRAINER空值没有用逗号分隔,因此计数不匹配。如果取消注释else if($field==null),该怎么办statementnope.not working它给出了一个错误:列计数与第1行的值计数不匹配。hello@Peter Etelej,您有任何提示吗?
$sql_insert = "\n\nINSERT INTO tsv_table (";
foreach ($columns as $column)
{
if($column == end($columns))
$sql_insert .= addslashes($column).")";
else
$sql_insert .= addslashes($column)." ";
}