Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open excel import table 1

Status
Not open for further replies.

patweb

Programmer
Apr 17, 2003
174
0
0
BE
I have excel files uploaded to my web. These files have fixed columns. I need to open these and the content (number of rows, records is always different) should be inserted in a existing table (fixed colums).

What is the best function to open and read the .xls file (fopen ?) and how to insert this in the table (insert sql or fwrite ?).
 
no problem, I found it myself :

- file() to get the data and put them in a array
- insert to put them in a table.

 
Hi patweb,
Can you please share this script ?
I'm looking for exactly this !

Would appreciate very much !!

Hans
 
Are you really reading xls format? Or are you using a CSV (comma separated values) file?

If it is a CSV you can use fgetcsv() to read each line.
 
Hi DRJ478,
It's an export through VBA from MSAccess, so I can export it the way I want.
Now I use csv and use this code, but gives an error :

"You have an error in your SQL syntax near ';Lastname;Bert;8745125;;;;Some street;1234 WJ;Hometown;024-' at line 1"

This is the actual first line in the csv file but has the Id 7 in the db. Is it the Id (Primary Key) or the Telphone nr '024-'should be more numbers here.. ?
Ideas ?
Code:
Code:
$csv = file("UploadedMatch.csv");  //this puts each line of the file into it's own element of the array "$csv"; $csv[0] is the field names, $csv[1] is the first row of data, etc. 
$count = count($csv);   //this is how many  rows are in that file 
for ($x = 1; $x<$count; $x++)   //starting at the first data row, going through all of it 
{ 
   list($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) = explode("\t",$csv[$x]);   //separates the data in the row on tabs into the variables 
    
   $query = "select Id from deelnemer where Id = $Id";   //this is just to see if that data row already exists in the table 
   $result =mysql_query($query) or die(mysql_error()); 
   while ($row = mysql_fetch_assoc($result)) 
   { 
      if ($row['Id']) 
      { 
         $update = true;   //the data row already exists in the table, we need to update it 
      } 
      else 
      { 
         $update = false;   //the data row isn't in the table, we need to insert it 
      } 
   } 
    
   if ($update) 
   { 
      //query goes here to update table info for that row (Id) 
      //execute the query 
   } 
   if (!$update) 
   { 
      $Upquery ="INSERT * INTO testtable values($Id, '$Naam', '$Voornaam', '$ASNNR', '$Datum_lid', '$URL', '$Vereniging', '$Adres', '$Postcode', '$Woonplaats', '$Telnr', '$Email', '$Oud_Sefnr'"; 
      //execute the query 
   } 

}
 
It appears to me that the fields in the CSV are not separated by TAB characters but by semicolons.
The output from the MySQL error suggests that.
 
Correct, is the explode("\t", for tab seperated ?
What is the correct syntax then ?

And can you tell me how this update query should look ?
Thanks for your time and support DRJ478 !
Appreciate it..
 
explodes' first parameter is the delimiter on which to separate. "\t" is the tab character. Any other char can be used, just be careful that it doesn't exist in the data content.

You read the entire file into an array. You could use fopen() and then fgetcsv to retrieve line by line. That's better, especially when the files get rather large. For small files it doesn't matter. The fgetcsv also takes the separator as an argument.

For the update I recommend the following:
when you generate the list() name all the variables exactly like the column names in the table.
When you check for the existence of the ID, just retrieve all the data, it's just one query that you have to do anyway. Just as you did, if the record exists, update it. I would only update the fields that are different from what the current data is - that's a real update. Just pushing identical data in is maybe easier, but less clean.
Here's the pseudo-op-code:
1. Iterate the current data
2. Compare the db value to the csv value
3. If different, add the field to the update SQL
4. Execute update query
Code:
if (is_array($row)){
   foreach ($row as $columnname => $value){
      if ($value != "$$columnname"){
          $update[] = "$columnname = '$$columnname'";
      }
   }
   $updateSQL = "UPDATE delnemeer SET ";
   $updateSQL .= implode(',',$update);
   $updateSQL .= " WHERE Id = $Id";
   # for now, just echo to test
   echo $updateSQL;
}

Untested code, no warranties.
 
Thanks a lot !
Got to quit now.. :-(
Will try your code and let know, ok?
Cool piece of code, I think..!
Cheers
 
Oke,
Still get an error
unexpected T_STRING, expecting ',' or ')' on line 9

This is line 9

list($Id, $Naam, $Voornaam, $ASNNR, $Datum lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud Sefnr) = explode("\;",$csv[$x]); //separates the data in the row on tabs into the variables

Here's the complete code:
Code:
<?php
mysql_select_db($database_ASN, $ASN);
$file = ("UploadedMatch.csv");
$csv = fopen($file , "r");  //this puts each line of the file into it's own element of the array "$csv"; $csv[0] is the field names, $csv[1] is the first row of data, etc. 
$count = count($csv);   //this is how many  rows are in that file 
for ($x = 1; $x<$count; $x++)   //starting at the first data row, going through all of it 
{ 
   list($Id, $Naam, $Voornaam, $ASNNR, $Datum lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud Sefnr) = explode("\;",$csv[$x]);   //separates the data in the row on tabs into the variables 
    
   $query = "select Id from deelnemer where Id = $Id";   //this is just to see if that data row already exists in the table 
   $result =mysql_query($query) or die(mysql_error()); 
   while ($row = mysql_fetch_assoc($result)) 
   { 
      if ($row['Id']) 
      { 
         $update = true;   //the data row already exists in the table, we need to update it 
      } 
      else 
      { 
         $update = false;   //the data row isn't in the table, we need to insert it 
      } 
   } 
    
   if ($update) 
   { 
   if (is_array($row)){
     foreach ($row as $columnname => $value){
       if ($value != "$$columnname"){
         $update[] = "$columnname = '$$columnname'";
      }
   }
   $updateSQL = "UPDATE deelnemer SET ";
   $updateSQL .= implode(',',$update);
   $updateSQL .= " WHERE Id = $Id";
   # for now, just echo to test
   echo $updateSQL;
}

   } 
   if (!$update) 
   { 
      $Upquery ="INSERT * INTO testtable values($Id, '$Naam', '$Voornaam', '$ASNNR', '$Datum lid', '$URL', '$Vereniging', '$Adres', '$Postcode', '$Woonplaats', '$Telnr', '$Email', '$Oud Sefnr'"; 
      //execute the query 
   } 

} 
//$filename2 = $csv . '.old';
 //   rename($csv, $filename2);

/* Closing connection */
mysql_close($ASN);
?>

Thanks for helping me out...

 
You have some major problems with your code.

Here's your code with my comments in [red]red[/red]
Code:
$file = ("UploadedMatch.csv");
$csv = fopen($file , "r");  //this puts each line of the file into it's own element of the array "$csv"; $csv[0] is the field names, $csv[1] is the first row of data, etc.
[red][b]No it doesn't, fopen OPENS the file for reading, the line above this one reads the entire file into an array. You don't need the fopen in this case.[/b][/red]
$count = count($csv);   //this is how many  rows are in that file
for ($x = 1; $x<$count; $x++)   //starting at the first data row, going through all of it
[red][b]You can put the count($csv) into the for statement[/b][/red]
{
   list($Id, $Naam, $Voornaam, $ASNNR, $Datum lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud Sefnr) = explode("\;",$csv[$x]);   //separates the data in the row on tabs into the variables
[b][red]You have spaces in some of your variables. That's why your getting the error. Why are you escaping the semi-colon ";"? Just write it as explode(';',$csv[$x])[/red][/b]
   $query = "select Id from deelnemer where Id = $Id";   //this is just to see if that data row already exists in the table
[b][red]Since you're going to updating the record if it exists, use "select *" instead of "select Id".[/red][/b]
   $result = mysql_query($query) or die(mysql_error());
   while ($row = mysql_fetch_assoc($result))
[red][b]Can you have more than one record in the database with the same Id? If not, why loop through 0 or 1 records? It would be better to do:[/b]

   $rc = mysql_count_rows($result);
   if ($rc == 1) { // row there, update
      $q = 'update deelnemer set ';
      $qend = " where Id='" . $Id . "'";
      $qtmp = array(); // make sure this is empty
      foreach ($row as $columnname => $value) //Note: if you didn't do a "select *" on the original query, you would have only the Id column in the array.
       if ($value != $$columnname) // don't really need the double quotes here
         $qtmp[] = "$columnname = '$$columnname'";
   }
   else { // row not there, insert
      $q = 'insert INTO testtable'; // the table you're inserting into is different that the table you're retrieving from???
      $qend = '';
      $qtmp = array();
      foreach (array(($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col) // I replaced the spaces in the columns with underscores "_"
          $qtmp[] = $col . "='" . $$col . "'";
   }
   $q .= implode(',',$qtmp) . $qend;
   echo $q . "<br>\n";
[b]Instead of your code.[/b]
[/red]

   {
      if ($row['Id'])
      {
         $update = true;   //the data row already exists in the table, we need to update it
      }
      else
      {
         $update = false;   //the data row isn't in the table, we need to insert it
      }
   }
    
   if ($update)
   {
   if (is_array($row)){
     foreach ($row as $columnname => $value){
       if ($value != "$$columnname"){
         $update[] = "$columnname = '$$columnname'";
      }
   }
   $updateSQL = "UPDATE deelnemer SET ";
   $updateSQL .= implode(',',$update);
   $updateSQL .= " WHERE Id = $Id";
   # for now, just echo to test
   echo $updateSQL;
}

   }
   if (!$update)
   {
      $Upquery ="INSERT * INTO testtable values($Id, '$Naam', '$Voornaam', '$ASNNR', '$Datum lid', '$URL', '$Vereniging', '$Adres', '$Postcode', '$Woonplaats', '$Telnr', '$Email', '$Oud Sefnr'";
      //execute the query
   }

If you have any questions, please post them.

Ken
 
Hi Ken,
Thanks for your time and support in this..
You probably figured out I'm a newbie with PHP..
I understand what you're saying, how would the first part (with all the comments) look to get it working ?
Appreciate very much !!

Hans
 
Changed the code but get error:
parse error, unexpected ',' in /home/virtual/site187/fst/var/ on line 29
line 29:
foreach (array(($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col)

Answering your qeustions:
Id is unique (Primary Key)
Inserting in testtabe is for testing purpose.
deelnemer is existing real table.


Complete code:
Code:
<?php
mysql_select_db($database_ASN, $ASN);
//$file = ("UploadedMatch.csv");
$csv = file("UploadedMatch.csv");  //this puts each line of the file into it's own element of the array "$csv"; $csv[0] is the field names, $csv[1] is the first row of data, etc.
echo $csv;
$count = count($csv);   //this is how many  rows are in that file 
for ($x = 1; $x<$count; $x++)   //starting at the first data row, going through all of it 
{ 
   list($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) = explode(';',$csv[$x]);   //separates the data in the row on tabs into the variables 
    
   $query = "select Id from deelnemer where Id = $Id";   //this is just to see if that data row already exists in the table 
   $result =mysql_query($query) or die(mysql_error()); 
   echo $result;
   while ($row = mysql_fetch_assoc($result)) 
   $rc = mysql_count_rows($result);
   if ($rc == 1) { // row there, update
      $q = 'update deelnemer set ';
      $qend = " where Id='" . $Id . "'";
      $qtmp = array(); // make sure this is empty
      foreach ($row as $columnname => $value) //Note: if you didn't do a "select *" on the original query, you would have only the Id column in the array.
       if ($value != $$columnname) // don't really need the double quotes here
         $qtmp[] = "$columnname = '$$columnname'";
   }
   else { // row not there, insert
      $q = 'insert INTO testtable'; // the table you're inserting into is different that the table you're retrieving from???
      $qend = '';
      $qtmp = array();
      foreach (array(($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col)
          $qtmp[] = $col . "='" . $$col . "'";
   }
   $q .= implode(',',$qtmp) . $qend;
   echo $q . "<br>\n";

//$filename2 = $csv . '.old';
 //   rename($csv, $filename2);

/* Closing connection */
mysql_close($ASN);
?>
 
My mistake, change
Code:
foreach (array[red](([/red]$Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col)
to
Code:
foreach (array($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col)

That's what I get for posting right after I get up... :)

Ken
 
Hi Ken ( Goodmorning then.. :))
Now I get unexpected $ at line 40 which is the end of the script.
Something not closed somewhere..?
I don't seen it.. you ?
 
Ok, here's your code with a few more fixes and the missing closing curly bracket. The easiest why to find mis-matched brackets, is to count opening vs closing brackets.
Code:
<?php
mysql_select_db($database_ASN, $ASN);
//$file = ("UploadedMatch.csv");
$csv = file("UploadedMatch.csv");  //this puts each line of the file into it's own element of the array "$csv"; $csv[0] is the field names, $csv[1] is the first row of data, etc.
echo '<pre>';print_r($csv);echo '</pre>'; [red]// this works much better for printing arrays[/red]
$count = count($csv);   //this is how many  rows are in that file
for ($x = 1; $x<$count; $x++)   //starting at the first data row, going through all of it
{
   list($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) = explode(';',$csv[$x]);   //separates the data in the row on tabs into the variables
    
   $query = "select * from deelnemer where Id = $Id";   //this is just to see if that data row already exists in the table [red]changed this to get all the fields[/red]
   $result =mysql_query($query) or die(mysql_error());
//   echo $result; // [red] this echo will not echo any data, just a resource id[/red]
   $rc = mysql_count_rows($result);
   if ($rc == 1) { // row there, update
      $q = 'update deelnemer set ';
      $qend = " where Id='" . $Id . "'";
      $qtmp = array(); // make sure this is empty
      foreach ($row as $columnname => $value) //Note: if you didn't do a "select *" on the original query, you would have only the Id column in the array.
       if ($value != $$columnname) // don't really need the double quotes here
         $qtmp[] = "$columnname = '$$columnname'";
   }
   else { // row not there, insert
      $q = 'insert INTO testtable'; // the table you're inserting into is different that the table you're retrieving from???
      $qend = '';
      $qtmp = array();
      foreach (array($Id, $Naam, $Voornaam, $ASNNR, $Datum_lid, $URL, $Vereniging, $Adres, $Postcode, $Woonplaats, $Telnr, $Email, $Oud_Sefnr) as $col)
          $qtmp[] = $col . "='" . $$col . "'";
   }
   $q .= implode(',',$qtmp) . $qend;
   echo $q . "<br>\n";
[b][red]}[/red] // this was the missing bracket [/b]
mysql_close($ASN);
?>

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top