cluce Posted June 24, 2008 Share Posted June 24, 2008 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/>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/ Share on other sites More sharing options...
cluce Posted June 24, 2008 Author Share Posted June 24, 2008 Is there a limit to the ampunt of records MYSQL can have? I tried to split up teh csv file and it wont add any more records. Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573273 Share on other sites More sharing options...
cluce Posted June 24, 2008 Author Share Posted June 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573278 Share on other sites More sharing options...
cluce Posted June 24, 2008 Author Share Posted June 24, 2008 now my code stops at record 127. ANybody haws any suggestions? thanks Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573327 Share on other sites More sharing options...
miracle_potential Posted June 24, 2008 Share Posted June 24, 2008 If you can wait an hour I already have an application that converts CSV files into MYSQL So yeah wait an hour or so and I'll post it up in another reply Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573350 Share on other sites More sharing options...
nashruddin Posted June 24, 2008 Share Posted June 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573354 Share on other sites More sharing options...
cluce Posted June 24, 2008 Author Share Posted June 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573357 Share on other sites More sharing options...
cluce Posted June 24, 2008 Author Share Posted June 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573361 Share on other sites More sharing options...
miracle_potential Posted June 24, 2008 Share Posted June 24, 2008 <? ########################################## # 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 Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573460 Share on other sites More sharing options...
cluce Posted June 24, 2008 Author Share Posted June 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/111683-solved-loop-insert-data-from-csv-file-issue/#findComment-573526 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.