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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.