我上传了一张CSV,用户必须告诉程序CSV文件中某些列的确切名称,以便它可以访问并获取数据以存储在数据库中。我有一些错误消息,只要他们没有填写必要的东西,它将把他们带回来形成修复他们犯下的错误。问题是它总是让我回来,我不知道为什么。
上传代码-------------------------------------------- -------------------------
try {
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo "I'm sorry, I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
function returnBack(){
header("Location:csvuploader/csvuploaderform.php?seterror=1");
exit;
}
if (isset($_POST['submit'])){
/*******************************GET NAME OF COLUMNS IN CSV FILE *********************************************************/
if (empty($_POST['files'])){
returnBack();
}
if (empty($_POST['firstname'])){
returnBack();
}
if (empty($_POST['lastname'])){
returnBack();
}if (empty($_POST['email'])){
returnBack();
}
if (empty($_POST['phone'])){
returnBack();
}
$file = $_FILES['files']['tmp_name'];
$handle = fopen($file , "r");
$fileop = fgetcsv($handle,1000,",");
$firstname_index = array_search($_POST['firstname'],$fileop);
if (empty($firstname_index)){
returnBack();
}
$lastname_index = array_search($_POST['lastname'],$fileop);
if (empty($lastname_index)){
returnBack();
}
$email_index = array_search($_POST['email'],$fileop);
if (empty($email_index)){
returnBack();
}
$phone_index = array_search($_POST['phone'],$fileop);
if (empty($phone_index)){
returnBack();
}
/***********************ASSIGN COLUMN VALUES TO ACCORDING VARIABLES AND INSERT THEN INTO CSV TABLE IN DB *************************************/
while (($fileop = fgetcsv($handle,1000,",")) !== false)
{
$firstname = $fileop[$firstname_index];
$lastname = $fileop[$lastname_index];
$email = $fileop[$email_index];
$phone = $fileop[$phone_index];
$insertdata = $DBH->prepare("INSERT INTO csv (firtname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')");
$insertdata->execute();
}
if ($insetdata){
echo "Successfully Uploaded. Thank you.";
}
}
感谢您的时间!
更新---------------------------
表单代码
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form method="post" action="csvuploader.php" enctype="multipart/form-data">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>CSV Uploader </strong></td>
</tr>
<tr>
<td>Enter Name of First Name Column </td>
<td>:</td>
<td><input name="fisrtname" type="text" ><?php
$setError=$_GET['seterror'];
if($setError == 1){
echo "<span class='errorMsg'>Need Exact Name</span>";
}
?> </td>
</tr>
<tr>
<td>Enter Name of Last Name Column </td>
<td>:</td>
<td><input name="lastname" type="text" ><?php
$setError=$_GET['seterror'];
if($setError == 1){
echo "<span class='errorMsg'>Need Exact Name</span>";
}
?></td>
</tr>
<tr>
<td>Enter Name of Email Column </td>
<td>:</td>
<td><input name="email" type="text" ><?php
$setError=$_GET['seterror'];
if($setError == 1){
echo "<span class='errorMsg'>Need Exact Name</span>";
}
?></td>
</tr>
<tr>
<td>Enter Name of Phone Number Column </td>
<td>:</td>
<td><input name="phone" type="text" ><?php
$setError=$_GET['seterror'];
if($setError == 1){
echo "<span class='errorMsg'>Need Exact Name</span>";
}
?></td>
</tr>
<tr>
<td width="294"><input type="file" name="files" /><?php
$setError=$_GET['seterror'];
if($setError == 1){
echo "<span class='errorMsg'>Select a File</span>";
}
?></td>
</tr>
<br />
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
答案 0 :(得分:1)
我不确定您的数据文件的外观,但您可能希望在trim
ed数组值上使用fgetcsv
,因为最后的值很可能包含一个行结束:< / p>
$fileop=fgetcsv($handle);
$fileop=array_map("trim",$fileop);
/*recognize index*/
while (($fileop=fgetcsv($handle))!==false)
{
$fileop=array_map("trim",$fileop);
$firstname=$fileop[$firstname_index];
/*insert into DB*/
}
修改强>:
哎呀,好像我错过了什么......你正在使用if(empty($firstname_index))
在您的代码中,但是,例如,如果array_search($_POST["firstname"],$fileop)
返回0(这是数组中的第一个索引),则您的empty($firstname_index)
将为TRUE
,您将被踢回来。
你应该使用
if($firstname_index===false)
而是(在所有四个索引中)。 PHP Document
编辑#2 :
此外,最好让你的标题匹配不区分大小写:
$fileop=fgetcsv($handle);
$fileop=array_map("strtoupper",array_map("trim",$fileop));
$firstname_index=array_search(strtoupper($_POST["firstname"]),$fileop);
if($firstname_index===false) returnBack();
/*and all four indexes*/