我在做什么 管理员将通过表单控件从下拉列表和CSV文件中选择一个测试编号(输入类型='文件'),然后点击提交按钮。
我正在创建一个表并将该CSV文件行存储在mysql DB中。
问题
上传CSV时,如果有' (撇号)符号或斜杠(/)然后它不读取该行。将跳过整行,但会上传剩余的行。
PHP
if(isset($_POST['uploadSubmit']))
{
//validate whether uploaded file is a csv file
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes))
{
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
//get test number and create table if not exists.
$testName = $_POST['test_number'];
$tableList = "show tables from online_exam like '%" .$testName. "%'";
$tableListResult = mysqli_query($con,$tableList);
if(mysqli_num_rows($tableListResult) == 0)
{
//echo $testName;
$createTestTable = "create table ".$testName."(id int PRIMARY KEY AUTO_INCREMENT,QNO int UNIQUE not null,QUESTION longtext not null,OPTION1 longtext not null,OPTION2 longtext not null,OPTION3 longtext not null,OPTION4 longtext not null,CORRECT_ANSWER varchar(20) not null,EXPLANATION longtext not null,IMAGE blob default null)";
//$x = mysqli_query($con,$createTestTable);
if(mysqli_query($con,$createTestTable))
{
//echo "<br>table created succesfully.";
//open uploaded csv file with read only mode
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
//skip first line
fgetcsv($csvFile);
//parse data from csv file line by line
while(($line = fgetcsv($csvFile)) !== FALSE)
{
//insert questions data into database
$con->query("INSERT INTO ".$testName." (QNO, QUESTION, OPTION1, OPTION2, OPTION3, OPTION4, CORRECT_ANSWER, EXPLANATION) VALUES (".$line[0].",'".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."','".$line[5]."','".$line[6]."','".$line[7]."')");
}
//close opened csv file
fclose($csvFile);
//$qstring = '?status=succ';
echo <<<XOD
<script type="text/javascript">
<!--
alert("CSV File Uploaded Successfully.");
//-->
</script>
XOD;
}
else
echo <<<XOD
<script type="text/javascript">
<!--
alert("Error Occurred While Creating Table.");
//-->
</script>
XOD;
}
else
{
echo <<<XOD
<script type="text/javascript">
<!--
alert("Sorry, Test Already Loaded.");
//-->
</script>
XOD;
}
}
else
{
//$qstring = '?status=err';
echo "<br>Error Occurred CSV File Loading.";
}
}
else
{
//$qstring = '?status=invalid_file';
echo "<br>Invalid CSV file.";
}
}
有人可以建议解决方案吗?