我有一个包含30000数据的csv文件我试图将所有数据上传到MySQL,但它没有将数据上传到mysql。
但如果尝试使用像100个数据这样的小数据,就会上传到数据库。
任何人都可以帮助我如何让它发挥作用
以下是我的代码
我的upload.php
<?php
include("db.php");
//Upload File
$sql="SELECT MAX(upload_id) FROM contact";
$result=mysqli_query( $MySQLi,$sql);
$row = mysqli_fetch_array($result);
$highest_id = $row[0];
//print $highest_id;
$uploadid = $highest_id+1;
//print $uploadid;
$dropdown = $_POST['DROP'];
print $dropdown;
if (isset($_POST['submit'])) {
//Import uploaded file to Database
$file = $_FILES['filename']['tmp_name'];
$handle = fopen($file,"r");
for ($lines = 0; $data = fgetcsv($handle,1000,",",'"'); $lines++) {
if ($lines == 0) continue;
for ($i = 0; $i <= 32; $i ++)
{
if (!isset($data[$i]))
$data[$i] = '';
}
$import="INSERT into contact
(name, contacttype, nationality, mobile1, country1,`mobile2`,`country2`,
email1,`email2`, phone, fax,`country3`, twon, area, restatus, retype,
reproject1, contactgroup, freezone1, gender,
dateofbirth, married, children, educationlevel, jobrole,
jobsector, driverslicense, language,media, media2, media3, media4, age,
salary, created, upload, image, companyname,
businesscategory, subcategory, upload_id)
values('$data[0]', '$data[1]','$data[2]','$data[3]',
'$data[4]', '$data[5]','$data[6]','$data[7]',
'$data[8]','$data[9]','$data[10]','$data[11]',
'$data[12]','$data[13]','$data[14]','$data[15]',
'$data[16]','$data[17]','$data[18]','$data[19]',
'$data[20]','$data[21]','$data[22]','$data[23]',
'$data[24]','$data[25]','$data[26]','$data[27]','$data[28]','$data[29]','$data[30]',
'$data[31]','$data[32]','$data[33]',".time().",'$data[35]','$data[36]','$data[37]', '$data[38]','$data[39]','$data[17]') ";
print $import;
mysqli_query( $MySQLi,$import) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);
//view upload form
}
fclose($handle);
print "Import done";
//header("Location: /uploadanddelete.php");
}else {
print "Import Failed";
}
?>
我的uploaddata.php
<p>Upload new csv by browsing to file and clicking on Upload </p>
<form enctype='multipart/form-data' action="upload.php" method="post" class='form-horizontal form-column form-bordered'>
<p>File name to import:
<input size='50' type='file' name='filename'><br /><br /> </p>
<input type="submit" name="submit" value="Upload"/>
</form>
答案 0 :(得分:1)
看起来不错,所以可能存在这样的问题:
是否已上传文件?
NO:检查php.ini中的upload_max_filesize和post_max_size
导入了一些行,或者当你有大文件时没有导入任何行?
NO:检查你的memory_limit(php.ini)你可以增加它:
ini_set('memory_limit','16M');
是:检查你的max_execution_time(php.ini)你可以通过这个增加它:
set_time_limit ($seconds)
如果没有帮助尝试调试代码的哪一部分是dyinig
答案 1 :(得分:0)
isset不是最可靠的检查。 strlen是一个更好的测试。
settype
是一种在没有if
因为没有错误并不意味着记录被插入。您必须检查rows_affected
。您真的不需要检查错误,只需要检查rows_affected。如果出现错误,则不会影响任何行。
保持插入的行数和错误将消除检查文件上载错误的必要性(就像你的print $ sql一样)。
替换:
if (!isset($data[$i]))
$data[$i] = '';
使用:
settype($data[$i],'string');
OR
if (strlen($data[$i]) < 1){$data[$i] = ''};
变化:
print $import;
mysqli_query( $MySQLi,$import)
or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);
要:
mysqli_query( $MySQLi,$import);
if (mysqli_affected_rows($MySQLi) !=1 || mysqli_errno($MySQLi) > 0)){
echo '<p class="warn">' . mysqli_error($MySQLi) . "<br/>$import</p>";
$errors++;
}
else{
$imported++;
echo "<p>$import</p>";
}
然后以结果结束:
echo "<p>$imported Records Imported with $errors Errors</p>";
<强> CSS 强>
.warn{color:#f00;}
p{padding:0;margin:.1em;}
body{background:#000;color:#ff0;font-size:.8em;}