PHP fgetcsv inserting only one row into Mysql table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlManz
    New Member
    • Jun 2012
    • 2

    PHP fgetcsv inserting only one row into Mysql table

    I am trying to upload a csv file into a Mysql database. for some reason it is only uploading the first row. I checked the data for any problem characters. I don't want to use the Load Into command because i want to upload some of fields only. here is my code
    Code:
    <?php
    CSV2DB('csvfile');
    ?>
    
    <?php 
    function get_numeric($val) { 
      if (is_numeric($val)) { 
        return $val + 0; 
      } 
      $d=substr($val,0,1);
      if(is_numeric($d)){
      	$d = preg_replace("/[^0-9]+/", "", $val);
      	return $d;
      }
        return 0; 
    } 
    ?> 
    
    <?php
    /**
    * Converts an uploaded CSV file to a row in a MySQL database table
    *
    */
    
    function CSV2DB($filename) {
    //Calling Database
    include_once('config.php');
    $dbc = new mysqli(DB_HOST, DB_USER, DB_PASS,DB_NAME);
    if (!$dbc) {
    die('connection failed:'.$mysqli->error());
    }
    
    if (FALSE == empty($_FILES[$filename]['tmp_name'])) {
    	$row = 1;
    	$handle = fopen($_FILES[$filename]['tmp_name'], "r");
    	while (($data = fgetcsv($handle, 1000, ",","\n"))!== FALSE) {
    		$num = count($data);
    		// echo "<p> $num fields in line $row: <br /></p>\n";
    		$row++;
    		for ($c = 0; $c < $num; $c ++) {
    			// Remove all commas
    			$data[$c] = str_replace(',',' ', $data[$c]);
    			//$disease_data[$data[$c]] = $data[$c + 1];
     			echo $data[$c] . "<br />\n";
    		}
    		$esi=trim($data[0]);
    		$address=trim($data[1]);
    		$street=substr($data[1],0,strpos($data[1],' ',1));
    		$street=get_numeric($street);
    		$city=trim($data[3]);
    		$state=trim($data[4]);
    		$zip=trim($data[5]);
    		$duns=trim($data[6]);
    		$mr=trim($data[7]);
    		$status=trim($data[8]);
    		$premise=trim($data[9]);
    		$metered=trim($data[13]);
    		$svcorders=trim($data[14]);
    		$custclass=trim($data[15]);
    		$ams=trim($data[16]);
    		//unset($data);
    		//Query Database;
    		$query="INSERT INTO esitable(ESI_ID, ADDRESS, STREET, CITY, STATE, ZIPCODE, DUNS, MR, STATUS, PREMISE, METERED, SVCORDERS, CUSTCLASS, AMS)".
    		"VALUES('$esi', '$address', '$street', '$city', '$state', '$zip', '$duns', '$mr', '$status', '$premise', '$metered', '$svcorders', '$custclass', '$ams')";
    		echo "$query";
    		$res=mysqli_query($dbc,$query) OR die(mysql_error());
    		//$res->close();
    	}
    	fclose($handle);
    }
    mysqli_close($dbc);
    }
    I appreciate any help. this is the output that i get
    Code:
    1.04E+16
    600 ASHLEY WILSON UNIT 400W SECLT
    
    SWEENY
    TX
    774800000
    7929441
    7
    Active
    Small Non-Residential
    ERCOT
    SWEENY
    SWEENY SUB
    N
    
    Small Non-Residential
    N
    INSERT INTO esitable(ESI_ID, ADDRESS, STREET, CITY, STATE, ZIPCODE, DUNS, MR, STATUS, PREMISE, METERED, SVCORDERS, CUSTCLASS, AMS)VALUES('1.04E+16', '600 ASHLEY WILSON UNIT 400W SECLT', '600', 'SWEENY', 'TX', '774800000', '7929441', '7', 'Active', 'Small Non-Residential', 'N', '', 'Small Non-Residential', 'N')
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it seems to be a problem with fgetcsv() (as it loops only once and that is the only function affecting that).

    are you sure that the "field enclosure character" is correct? (or any other setting used)

    turn up error reporting and display in case there was an error.


    tip: for that kind of task, a Prepared Statement is better suited.

    PS. line #67 can be deleted. you must not close the connection before you’re done with all queries.

    Comment

    • AlManz
      New Member
      • Jun 2012
      • 2

      #3
      thanks. I figured out the error. the file contained same values for a field that is declared as primary key in the table.

      Comment

      Working...