Jump to content

[SOLVED] loop insert data from csv file issue


cluce

Recommended Posts

I am trying to insert 417 records from a csv file.  The problem I am having is it is only inserting about 250 records. Does anybody know why my query stopped inserting records into the database early? 

 

my code:

 


<?php
$counter = 1; //initialize counter

include'db.php';

include'salt.php';

$handle = fopen("UserInfo.csv","r");
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
   $username = $data[0];   
   $password = $data[1];  
   $f_name = $data[2];   
   $l_name = $data[3];   
   $email = $data[4];
   $last = $data[5];
   $failed = $data[6];
   $priv = $data[7];
   $company = $data[8];
   $division_number = $data[9];
   
   $sql = "INSERT INTO employees () VALUES ('')"; 
    
   mysqli_query($mysqli, $sql);   

   $counter++;
   echo 'SQL='.$sql.'<br/>';  
}
?>

I also get an error when I try to manually add in a record from the admin page:

 

Error

SQL query: 

 

INSERT INTO `test`.`employees` (

 

`EmployeeID` ,

`username` ,

`password` ,

`f_name` ,

`l_name` ,

`email` ,

`last_login` ,

`failed_logins` ,

`privilege` ,

`first_name` ,

`last_name` ,

`address` ,

`city` ,

`state` ,

`zip` ,

`phone` ,

`altphone` ,

`company` ,

`supervisor_name` ,

`division_number`

)

VALUES (

'256', 'test', 'test', 'test', 'test', NULL , NULL , NULL , NULL , '', '', '', '', '', '', '', '', '', '', ''

)

 

MySQL said: 

 

#1062 - Duplicate entry '255' for key 1

 

 

 

yes, I can wwait an hour. can you please let me know when you post it. thanks.  as far as inserting nothiing.......I deleted the values for secuirty reasons but I didnt think I really needed to.  thanks again for your replies. I really need to get this to work.

looks like there is a duplicate key when your loop stops. by the way, what is this line?

 

$sql = "INSERT INTO employees () VALUES ('')"; 
mysqli_query($mysqli, $sql);

 

you insert nothing to the table.

 

actually that gave me the error when I tried to insert A record(meaning one) 256 manually. as you can see in the first value of the error....... I am getting frustrated with this  :-[

<?
##########################################
# read the data file into an array
# each element of this array contains all of the information for each item

$data_file = "csv.dat";

$line_array = file($data_file);

########################################
# make your database connection here
mysql_connect("localhost", "username", "password")or die("didnt connect1");
mysql_select_db("database")or die("didnt connect2");

####################################################
# loop through the first array

$number_of_items = count($line_array);

$i =0;
for($i=0;$i<$number_of_items;$i++) {

########################################
# explode each element into a new array using the comma as the delimiter

$temp_array = explode(",",$line_array[$i]);

###########################################
# make your mysql query here and
# insert each field from the temp array into the proper mysql fields like $temp_array[1] in order of your rows affected

$sql = mysql_query("INSERT INTO table (rows you want to effect)  VALUES('$temp_array[2] ) ") or die(mysql_error()); 
}
echo "inserted/inserting";

?>

 

There you go fella play with that for a while I got that from here so thanks to whoever it was that posted it originally  ;D

thanks a million. that still didnt solve it. BUT I figured out the problem because of trying out the new code. I had to drop the table and create a new one. I am guessing it had something to do with to many records or or dupi;lcate keys or something . I am not sure.  problem SOLVED

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.