Jump to content

can I insert a file into a database other than a csv file


cluce

Recommended Posts

I have an issue with uploading and keeping the same data in my excel spreadsheet when I convert it to a csv file.  First, I tried to insert the excel spreadsheet into the database using the following code:

 


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

include'db.php';

include'salt.php';

$handle = fopen("triaport.xls","r");
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
   $company = $data[0];
   $division_number = $data[1];
   $username = $data[2];   
   $password = $data[3];  
   $f_name = $data[4];   
   $l_name = $data[5];   
   $email = $data[6];   
   $priv = $data[7];
   
   $sql = "INSERT INTO employees (deleted for security reasons) VALUES (')"; 
    
   mysqli_query($mysqli, $sql);   

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

 

which works fine with a csv file. But the problem is two of the columns in my table require leading zeros and csv file removes the leading zeros. I tried to use an excel spreadsheet but it does not work with my loop which inserts Null values in my table. I have about 500 records so manually adding leading 0's in the csv file would be tedious.  A csv file removes all formatting which changes the actual data that I need to insert in the databse.

 

Does anybody know any other method I can use to insert a file into the MYSQL database?  Or know how to use another type of file besides a csv file that would work?

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.