gpt4 book ai didi

php - 将csv文件数据插入mysql表时遇到麻烦

转载 作者:行者123 更新时间:2023-11-29 23:51:36 24 4
gpt4 key购买 nike

我想从 html 中的文件输入元素获取一个文件。我在输入类型文件中设置了名称属性。但我无法得到它。请帮我解决这个问题。我的 php 编码是:

function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 10000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header,$row);
}
fclose($handle);
}
return $data;
}
if(isset($_POST['submit']))
{
$count=0;
$file_name=basename($_FILES['csv']['name']);
$upload_path="upload_files/".time()."_".$file_name;
move_uploaded_file($_FILES['csv']['tmp_name'], $upload_path);
$file_path = 'upload_files/'.$file_name;
$result = csv_to_array($file_path);
foreach($result as $results)
{
$question=addslashes($results['question']);
$que_img=addslashes($results['question_img']);
$img_for=addslashes($results['img_for']);
$option1=addslashes($results['option1']);
$option2=addslashes($results['option2']);
$option3=addslashes($results['option3']);
$option4=addslashes($results['option4']);
$corrct_ans=addslashes($results['correctanswer']);
$justification=addslashes($results['justification']);
$subject_id=$_POST['subjectid'];
$pgroup=addslashes($results['pgroup']);
$karea=addslashes($results['karea']);
$mock=addslashes($results['mock']);
$email_ins = "insert into online_questions(question,question_img,img_for,option1,option2,option3,option4,correctanswer,justification,subjectid,pgroup,karea,mock) values ('$question','$que_img','$img_for','$option1','$option2','$option3','$option4','$corrct_ans','$justification','$subject_id','$pgroup','$karea','$mock')";
if(mysql_query($email_ins))
{
$count++;
}
}
if($count!=0)
{
header("location:upload_questions.php?msg=success");
}
else
{
//header("location:upload_questions.php?msg=error");
$rightside .= '<div class="msg-error"><h4>Error:'.mysql_error().'</h4></div>';
}
}
else
{
$rightside .= '<form method="POST">
<fieldset><legend>Upload questions</legend>
<ul class="align-list"><li><label for="test-username">Select Subject :</label>
<select name="subjectid" class="required">
<option>-----Select-----</option>
<option value="8">PMP Preparatory Program</option>
<option value="9">Primavera</option>
<option value="10">Microsoft Project [MSP]</option>
</select></li>
<li><label for="test-username">Upload CSV : </label><input name="csv" type="file" /></li>
<li><label for="test-username"></label><input type="submit" name="submit" value="Submit" /></li>
</fieldset></form>';
}

如何获取上述html编码的文件和文件名。

最佳答案

试试这个:已编辑

<?php
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 10000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header,$row);
}
fclose($handle);
}
return $data;
}
if(isset($_POST['submit']))
{
$count=0;
$file_name=$_FILES['csv']['name'];
echo $_FILES['csv']['name'];
$upload_path="upload_files/".time()."_".$file_name;
move_uploaded_file($_FILES['csv']['tmp_name'], $upload_path);
$file_path = 'upload_files/'.$file_name;
echo $file_path;
$result = csv_to_array($file_path);
foreach($result as $results)
{
$question=addslashes($results['question']);
$que_img=addslashes($results['question_img']);
$img_for=addslashes($results['img_for']);
$option1=addslashes($results['option1']);
$option2=addslashes($results['option2']);
$option3=addslashes($results['option3']);
$option4=addslashes($results['option4']);
$corrct_ans=addslashes($results['correctanswer']);
$justification=addslashes($results['justification']);
$subject_id=$_POST['subjectid'];
$pgroup=addslashes($results['pgroup']);
$karea=addslashes($results['karea']);
$mock=addslashes($results['mock']);
$email_ins = "insert into online_questions(question,question_img,img_for,option1,option2,option3,option4,correctanswer,justification,subjectid,pgroup,karea,mock) values ('$question','$que_img','$img_for','$option1','$option2','$option3','$option4','$corrct_ans','$justification','$subject_id','$pgroup','$karea','$mock')";
if(mysql_query($email_ins))
{
$count++;
}
}
if($count!=0)
{
header("location:upload_questions.php?msg=success");
}
else
{
//header("location:upload_questions.php?msg=error");
$rightside .= '<div class="msg-error"><h4>Error:'.mysql_error().'</h4></div>';
}
}
else
{
$rightside .= '<form method="post" accept-charset="utf-8" enctype="multipart/form-data">
<fieldset><legend>Upload questions</legend>
<label for="test-username">Select Subject :</label>
<select name="subjectid" class="required">
<option>-----Select-----</option>
<option value="8">PMP Preparatory Program</option>
<option value="9">Primavera</option>
<option value="10">Microsoft Project [MSP]</option>
</select>
<label for="test-username">Upload CSV : </label><input name="csv" type="file" />
<label for="test-username"></label><input type="submit" name="submit" value="Submit" />
</fieldset></form>';
}
echo $rightside;

?>

我已经删除了 li,您可以添加它们。问题出在您的表单标记上,我已经更改了它。

<form  method="post" accept-charset="utf-8" enctype="multipart/form-data">

关于php - 将csv文件数据插入mysql表时遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25615647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com