0

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); 
 }
8
  • 1
    You are not doing any error checking in your query. Add it. The manual shows how. php.net/manual/en/function.mysql-query.php The manual will also show you alternatives to the mysql library, which is outdated. Commented Nov 1, 2012 at 7:28
  • @Mr.Alien @Pekka, I have error_reporting(E_ALL); ini_set('display_errors','1'); at the top of my document. I'm getting no errors whatsoever. Nor does the echo at the bottom of the loop display. Commented Nov 1, 2012 at 7:31
  • 1
    @Gordie use mysqli_error() for turning on sql errors, php.net/manual/en/mysqli.error.php Commented Nov 1, 2012 at 7:35
  • 1
    Because one of your database field is primary, you cannot insert duplicate value in the primary field, so you are getting this error Commented Nov 1, 2012 at 7:48
  • 1
    @Mr.Alien It really was as simple as that. I thought the field I set for primary was unique, turns out it's not. Sorry I'm an idiot. Make it an answer and I'll choose it. Free points. Commented Nov 1, 2012 at 7:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.