所以我做了这个上传.csv文件然后将数据插入mysql数据库的文件,但由于某种原因,它增加了一个额外的" (引用)标记)在CSV文件的数据开头。 所以对于某些事情: - 笔记本电脑,Apple Mac,750,100,50 这样,它会以下列方式插入数据库。
Category Product Name Price1 Price2 Price3
"Laptop Apple Mac 750 100 50
我不确定原因,但在笔记本电脑之前添加了引号。
我该怎么做才能解决这个问题? 这是我的代码: -
<?php
session_start();
include '../inc/inc.functions.php';
include '../dbconnector.php';
include '../dbpdo.php';
include '../inc/inc.config.php';
if((isset($_SESSION['admin'])) && ($_SESSION['admin'] == 1))
{
$adminusername = $_SESSION['username'];
$date=date('Y-m-d');
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//prepare the statement for the insertion of the record via PDO
try{
global $conn;
$statement = $conn->prepare("INSERT INTO products(category,productname,baseprice,basepricewd,basepricenw,addedby,addedon) VALUES (?,?,?,?,?,?,?)");
//loop through the csv file and insert into database
while ($data = fgetcsv($handle,1000,",","'"))
{
if ($data[0]) {
$statement->execute(array(
$data[0],
$data[1],
$data[2],
$data[3],
$data[4],
$adminusername,
$date));
}
}
//
}//try
catch(PDOException $e)
{
echo $e->getMessage();
}
//redirect
header('Location: admin_importproducts_home.php?success=1'); die;
// echo "Products imported successfully";
}
}
else
{
header('Location:http://domain.co.uk/' . $config["admindir"] . '/adminlogin');
}
?>
CSV文件
Laptop,Apple Mac,750,100,50
CSV图片链接: - http://i.imgur.com/WkpYeeU.png
欢迎任何建议。
答案 0 :(得分:0)
str_replace('"', '', $data[0] );
str_replace('"', '', $data[1] );
str_replace('"', '', $data[2] );
str_replace('"', '', $data[3] );
str_replace('"', '', $data[4] );
will solve the problem.