我陷入了一些死胡同。我有csv文件,用逗号分隔,但我无法将数据导入数据库。是什么导致这个?没有错误,没有数据导入
导入代码:
<form enctype="multipart/form-data" method="POST">
<input name="csv" type="file">
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$i=0;
require "connection.php";
if (is_uploaded_file($_FILES['csv']['tmp_name'])) {
echo "<h3>" . "File ". $_FILES['csv']['name'] ." uploaded successfully." . "</h3>";
}
$handle = fopen($_FILES['csv']['tmp_name'], "r");
$import=$db->prepare("INSERT INTO adherence(
dateandtime,
lastname,
firstname,
paidtime,
approvedtime,
notadhering)VALUES(
?,?,?,?,?,?)");
while (($data = fgetcsv($handle, 10000,',')) !== FALSE) {
if($i>0) {
$data = str_replace('"', '', $data);
$myDate = date("Y/m/d",strtotime(str_replace('/','-',$data[0])));
$import->bindParam(1, $myDate, PDO::PARAM_STR);
$import->bindParam(2, $data[1], PDO::PARAM_STR);
$import->bindParam(3, $data[2], PDO::PARAM_STR);
$import->bindParam(4, $data[3], PDO::PARAM_STR);
$import->bindParam(5, $data[4], PDO::PARAM_STR);
$import->bindParam(6, $data[5], PDO::PARAM_STR);
$import->execute();
}
$i++;
}
$removal=$db->prepare("delete FROM adherence WHERE approvedtime = '0' OR notadhering IS NULL");
$removal->execute();
fclose($handle);
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
notadhering)VALUES(
尝试添加空格
notadhering) VALUES(
我不知道如何编写PHP代码。格式化后,sql命令应为
INSERT INTO adherence(
dateandtime,
lastname,
firstname,
paidtime,
approvedtime,
notadhering)
VALUES(
#2015/04/01 00:00#,
'lastname',
'name',
8,
0.14972222,
0.31222222
);
我很快就学会了PHP(幽默,只是观看了连接语法)。我认为PHP代码应该是
$myDate = "#{$myDate}#";
$lname = "'{$data[1]}'";
$fname = "'{$data[2]}'";
$import->bindParam(1, $myDate, PDO::PARAM_STR);
$import->bindParam(2, $lname, PDO::PARAM_STR);
$import->bindParam(3, $fname, PDO::PARAM_STR);
$import->bindParam(4, $data[3], PDO::PARAM_STR);
$import->bindParam(5, $data[4], PDO::PARAM_STR);
$import->bindParam(6, $data[5], PDO::PARAM_STR);