csv file upload in php inserts only first row in database - php

I want to upload csv file in mysql database in php. I am using,
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while (($data = fgetcsv($handle,1000,',','"')) !== FALSE) {
// insert into database...
}
fclose($handle);
But, it inserts only first row of the file.
EDIT:
when I am trying to print_r $data within while loop, then also it's giving me only one row.

Related

Read a CSV file and upload data to correct table in PHP

I have a SQL database with 5 tables and I also have 5 CSV files, one for each of those tables.
I am struggling to create a PHP script that can be used to read each files and then upload the data into the correct table.
How can I go about this?
URL - http://php.net/manual/en/function.fgetcsv.php
<?php
$filename = "test.csv";
//Open the csv file in the read mode
if (($handle = fopen($filename, "r")) !== FALSE) {
//loop into the each row and do the stuff
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//$data will have all the related columns
//Make sure you will use INSERT query here
}
fclose($handle);
}
If you want to write to specific tables there are many way to do so
Create 5 files where in each and every file while loop you will have that respective table name.
Eg : Think that you want to insert for customers.csv file
Then in while loop you may use the following way
while(/*Code*/){
//INSERT INTO customers (col1, col2) VALUES (val1, val2);
}
In case if your lazy than in one file only you can make a switch operation as follows
$filename = 'customers.csv';
switch( $filename ) {
case 'customers.csv':
while(){
//Insert
}
break;
case 'products.csv':
while(){
//Insert
}
break;
}

While uploading csv file in mysql using php

While I am uploading a csv file using php coding in MySQL ,all rows and columns were inserting .I have 670 data in it but it is inserting 1000 rows,I want only 670 rows to be added
Please check below code hope this help you
$count=0;
while(($data = fgetcsv($inputfp, 4096, ',')) !== FALSE) {
if($count==670) Break;
else{
//your inserting code here
}
$count++;
}

PHP - insert into mutiple tables, each table with identifcal fields

I'm going to explain with my best efforts what my goal is here. Everything I've searched for online hasn't been relevant enough for me to gain an idea.
First off, this is a PHP assignment where we have to load CSV files into a MySQL database.
Now, each table (total of 4) have the exact same field values. What I am trying to accomplish is using a for each loop that populates each table with the information from the CSV file. I know I can do this by having a while loop for each table and CSV file but I'm trying to go above the requirements and learn more about PHP. Here is my code for what I'm trying to accomplish:
$files = glob('*.txt'); // .txt required extension
foreach($files as $file) {
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle,4048, ",")) !== FALSE) {
echo $data[0]; // making sure data is correct
$import = "INSERT INTO".basename($file)."(id,itemName,price) VALUES('$data[0]','$data[1]','$data[2]')";
multi_query($import) or die (mysql_error());
}
fclose($handle);
}
else {
echo "Could not open file: " . $file;
}
}
Each CSV file contains the id, itemName and price. Hopefully this is understandable enough. Thank you
The way you are importing data into MySQL is OK for small volume of data. However, if you are importing huge volumes(thousands of rows), the best way would be to import it directy into MySQL is by using infile. Fo example:
LOAD DATA LOCAL INFILE '/path/to/your_file.csv'
INTO TABLE your_table_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"' LINES
TERMINATED BY '\n' (id, itemName, price)
That's a smarter way to import your CSV data :)

PHP MYSQL import CSV and then compare and remove redundant entries

I am stuck with a peculiar issue here. I have a script that basically imports a CSV file into a database using fgetcsv() in php. There is no problem in doing this at all and I am able to update old entries as well using MySQL syntax ON DUPLICATE KEY UPDATE (I am in no way a MySQL expert, hence me asking here).
Here is that part of the code:
$handle = fopen($file,"r");
fgetcsv($handle, 1000, ",");//skip first row since they are headers
while(($fileop = fgetcsv($handle, 1000, ",")) !== false) //read line by line into $fileop
{
//read array values into vars
$item1 = $fileop[0];
$item2 = $fileop[1];
$key = $fileop[2];
// and a couple more
// now INSERT / UPDATE data in MySQL table
$sql = mysql_query("INSERT INTO table (item1,item2,key)
VALUES ('$item1','$item2','$key')
ON DUPLICATE KEY UPDATE item1='$item1',item2='$item2'");
}
This all works fine. What I am stuck with is the fact that some entries may have been removed from the actual CSV (as in the key may no longer be existant). What I would like to do is remove the entries from the MySQL table that are no longer present in the CSV.
Meaning if $key is gone from CSV also remove that row in the database table. I suppose I would do it before I run the Insert / Update query on the MySQL table?
I would appreciate any help guys.
Just keep an account of your keys.
Save every $key in an array in your while, and in the end run a query that says
DELETE FROM tabel WHERE key NOT IN (listofcommaseparatedkeysgoeshere)
$arrayThatYouNeedToTest = array();
$handle = fopen($file,"r");
fgetcsv($handle, 1000, ",");//skip first row since they are headers
while(($fileop = fgetcsv($handle, 1000, ",")) !== false) //read line by line into $fileop
{
//read array values into vars
$item1 = $fileop[0];
$item2 = $fileop[1];
$key = $fileop[2];
// and a couple more
// now INSERT / UPDATE data in MySQL table
$sql = mysql_query("INSERT INTO table (item1,item2,key)
VALUES ('$item1','$item2','$key')
ON DUPLICATE KEY UPDATE item1='$item1',item2='$item2'");
$arrayThatYouNeedToTest[] = $key;
}
$stringThatYouNeedToInspect = implode(",",$arrayThatYouNeedToTest);
$queryYouREALLYneedToCheckFirst = "DELETE FROM tabel WHERE key NOT IN (".$stringThatYouNeedToInspect.")";
//$result = mysql_query($queryYouREALLYneedToCheckFirst);
I do something very similar to this with an affiliate website - having just under 500,000 products.
In your database, simply add another column named "update_flag" or something similar. Set the default to be 0. As you add items from the CSV file, set the update_flag to be "1". In your 'on duplicate statement', set the filed to be "2". I also went and added 2 other fields: "date_added" and "date_updated".
After your import is complete, you can count the old items (to be deleted), newly added items, and those that have been updated. You can then simple delete from table where update_flag = 0
I hope this helps.

populating database from csv file using php

How would I go about populating a database from info in a csv file using PHP code? I need to practice using php to make database calls but at the moment, all I have access to is this csv file...
Design Considerations:
You probably don't want to load the entire file into memory at once using a function like file_get_contents. With large files this will eat up all of your available memory and cause problems. Instead do like Adam suggested, and read one line at a time.
fgetcsv at php manual
//Here's how you would start your database connection
mysql_connect($serverName, $username, $password);
mysql_select_db('yourDBName');
//open the file as read-only
$file = fopen("file.csv", "r");
// lineLength is unlimited when set to 0
// comma delimited
while($data = fgetcsv($file, $lineLength = 0, $delimiter = ",")) {
//You should sanitize your inputs first, using a function like addslashes
$success = mysql_query("INSERT INTO fileTable VALUES(".$data[0].",".$data[1].")");
if(!$success) {
throw new Exception('failed to insert!');
}
}
just do it through phpmyadmin: http://vegdave.wordpress.com/2007/05/19/import-a-csv-file-to-mysql-via-phpmyadmin/
Use the built-in PHP functions to read the CSV and write an output file. Then you can import the SQL into your database. This should work with any type of database.
Don't forget to escape any strings you are using. I used sqlite_escape_string() for that purpose in this example.
$fd = fopen("mydata.csv", "r");
$fdout = fopen("importscript.sql","w");
while(!feof($fd))
{
$line = fgetcsv($fd, 1024); // Read a line of CSV
fwrite($fdout,'INSERT INTO mytable (id,name)'
.'VALUES ('.intval($line[0]).",'".sqlite_escape_string($line[1])."');\r\n";
}
fclose($fdout);
fclose($fd);

Categories