简体   繁体   中英

How i can import a file(csv/excel) with partial data to table in database through phpmyadmin?

I needed from my client updated information to fill a clients table.

I exported the table that i wanted to an excel file, and asked them to fill with the new information (it was only a column that i needed updated) and they've sent me back the file.

Now I want to import that information back to my table column.
Trial and error many times, converted to csv the excel file and imported through phpmyadmin.

But it didnt update any column.

What am I doing wrong?

如果您只需要从CSV数据生成UPDATE语句,则可以看一下我的FOSS CSV工具CSVFix ,它可以执行此操作以及更多其他操作,而无需编写任何代码(PHP或其他方法)。

If you have the file in a .csv and you know some PHP, you can just write a script which loops through the file and inserts/updates the records in the database.

For example, lets say that each line in your csv is structured like this:

id,name,address,email,date

Eg:

1,bob smith,344 abc street,test@example.com,2009-04-01

You could loop through it in this way:

<?php
$data=file_get_contents('your-file.csv');
//Split the file and get an array representing the lines/rows in the .csv
$rows=explode("\n",$data); 

foreach ($rows as $row)
{

   //Remove any excess whitespace from start and end of the row:
   $row=trim($row);
   $id=$row[0];
   $name=$row[1];
   $address=$row[2];
   $email=$row[3];
   $date=$row[4];
   mysql_query("UPDATE TABLE SET name='$name',....);
}
?>

PHP has a function called fgetcsv() to parse CSV files.

You could use that to loop through your CSV file and create MySQL update strings. Which you could execute either through mysql_query() or just to copy and paste into the Query window in PHPMyAdmin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM