Php SQL查询不在循环中工作
我有一个从外部文件中提取几行数据的脚本,然后我需要将这些行中的每一行上传到数据库。我在循环中放置了一个查询,但它什么也不做。即使打开了错误报告,我也一无所获。我检查了数据库,但没有上传任何内容 正确的方法是什么? 提前谢谢 我的代码:Php SQL查询不在循环中工作,php,sql,database,loops,insert,Php,Sql,Database,Loops,Insert,我有一个从外部文件中提取几行数据的脚本,然后我需要将这些行中的每一行上传到数据库。我在循环中放置了一个查询,但它什么也不做。即使打开了错误报告,我也一无所获。我检查了数据库,但没有上传任何内容 正确的方法是什么? 提前谢谢 我的代码: $size = $_FILES['file']['size']; $filename = $_FILES['file']['tmp_name']; $max_filesize = 100000; if($size > $max_filesize) //
$size = $_FILES['file']['size'];
$filename = $_FILES['file']['tmp_name'];
$max_filesize = 100000;
if($size > $max_filesize) //check file size
die('File is too large');
if(file_exists($filename)){
$fp = fopen($filename, "r");
$str = fread($fp, filesize($filename)); //file text stored in variable
$br = "\n"; //search for new row
$rows = substr_count($str,$br); //number of rows
echo "Rows: ". $rows."<br />";
$row = explode( "\n",$str);
$x=0;
while($x<=$rows)
{
$field = $row[$x];
$exp = explode("|",$field);
$case_number = $exp[1];
$unknown1 = $exp[2];
$chapter = $exp[3];
$filed = $exp[8];
//tons more variables, removed for readability
$addrow = mysql_query("INSERT INTO records (case_number, wild1, chapter, filed) VALUES('".$case_number."', '".$unknown1."', '".$chapter."', '".$filed."')");
if($addrow)
{
echo "<p style=\"font-weight:bold\">Row ".$x." uploaded</p>";
}else{
die(mysql_error());
}
$x++;
}
fclose($fp);
}
$size=$\u文件['file']['size'];
$filename=$\u文件['file']['tmp\u名称'];
$max_filesize=100000;
如果($size>$max\u filesize)//检查文件大小
模具('文件太大');
如果(文件_存在($filename)){
$fp=fopen($filename,“r”);
$str=fread($fp,filesize($filename));//存储在变量中的文件文本
$br=“\n”//搜索新行
$rows=substr\u count($str,$br);//行数
回显“行:”.$Rows.“
”;
$row=分解(“\n”,$str);
$x=0;
虽然($x您已经提到了键“PRIMARY”的错误重复条目
,因此在设置为PRIMARY
的列中不能有重复的值。您在查询中没有进行任何错误检查。添加它。该手册说明了如何添加。该手册还将向您展示mysql库的替代方案,该库已经过时。@Mr.Alien。)@Pekka,我的文档顶部有error\u reporting(E\u ALL);ini\u set('display\u errors','1');
。我没有收到任何错误。循环底部的回声也没有显示。@Gordie usemysqli\u error()
用于打开sql错误,因为您的一个数据库字段是主字段,所以您不能在主字段中插入重复值,因此您将得到以下结果error@Mr.Alien其实就这么简单。我以为我为小学设置的领域是独一无二的,但事实并非如此。对不起,我是个白痴。回答这个问题,我会选择它。免费积分.