I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

SQL Query Not Working In Loop

I have a script that extracts several rows of data from an external file, I then need to upload each of these rows to a database. I put a query inside my loop but it does nothing. even with error reporting turned on I get nothing. I check the database but nothing is being uploaded.

What is the correct way to do this? Thanks in advance!

My Code:

$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); 
 }

Upvotes: 0

Views: 246

Answers (1)

Mr. Alien
Mr. Alien

Reputation: 157314

You have mentioned the error Duplicate entry for key 'PRIMARY' so you cannot have a duplicate value in the column which is set to PRIMARY.

Upvotes: 1

Related Questions