Skip to main content

UPLOAD A CSV FILE TO DATABASE USING PHP

<?php 
error_reporting(1); 
$server = "localhost";
$user_name = "****";//database username 
$password = "****";//database password 
$database = "sample";//database name 
$connection = mysqli_connect("$server","$user_name","$password","$database") or die("Error " . mysqli_error($connection)); 
$csvfile = $_FILES['csvfile']['name']; 
$ext = pathinfo($csvfile, PATHINFO_EXTENSION); 
$base_name = pathinfo($csvfile, PATHINFO_BASENAME); 
if (isset($_POST['submit'])) 
{ 
 if($ext == "csv") 
 { 
 if (is_uploaded_file($_FILES['csvfile']['tmp_name'])) 
 { 
 echo "<h1>" . "File ". $_FILES['csvfile']['name'] ." uploaded successfully." . "</h1>"; 
 } 
 $handle = fopen($_FILES['csvfile']['tmp_name'], "r"); 
 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
 { 
 $import="INSERT INTO csv(firstname,email) VALUES('$data[0]','$data[1]')"; 
 $result = mysqli_query($connection, $import) or die("Error in Selecting " . mysqli_error($connection)); 
 } 
 fclose($handle); 
 echo "Import done"; 
 } 
 else 
 { 
 echo " Check Extension. your extension is ." . $ext; 
 } 
}?>

<html>
<body>
    <form method="post" enctype="multipart/form-data">

        <table>

            <tr>

                <td>Upload a CSV File:-  </td><td><input type="file" value="Upload CSV Format" name="csvfile" required />

                    <input type="submit" value="Upload" name="submit" /></td>

            </tr>

        </table>

    </form>
</body>
</html>



NOTE:

Create a csv(firstname,email) table inside the sample database and copy the above code and run it. 

Comments

Popular posts from this blog

Send Email(with subject,body,attachment) using SSMTP through Terminal in linux?

1.Install ssmtp as              $ sudo apt-get update && sudo apt-get install ssmtp 2.Configure smtp file by editing the following file as, $ sudo gedit /etc/ssmtp/ssmtp.conf Update file with the following: AuthUser=Sendermail@gmail.com AuthPass=SenderPasswordforGMAIL FromLineOverride=YES mailhub=smtp.gmail.com:587 UseSTARTTLS=YES Now ssmtp.conf file should look like as the following one, AuthUser=Sendermail@gmail.com AuthPass=SenderPasswordforGMAIL # # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=postmaster # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=smtp.gmail.com:587 # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=tele-desktop118 # A...

Mark all checkboxes based using one checkbox

< html > < body > < label >< b > Year* </ b ></ label > < input type= "checkbox" id= 'year' class= "yearcheckall" value= "0" >< b > All </ b > < input type= "checkbox" name= "year[]" class= "year" value= "1" required > 1st Year < input type= "checkbox" name= "year[]" class= "year" value= "2" required > 2nd Year < input type= "checkbox" name= "year[]" class= "year" value= "3" required > 3rd Year < input type= "checkbox" name= "year[]" class= "year" value= "4" required > 4th Year </ body > </ html > < script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" ></ script > < script > $ ( ...

What is CronTab? What does it do?

What if you want to execute a file daily at particular time ? Running it manually daily is  absolutely not a good idea ..!   Then What do you do ? For this kind of manual works , To Automate it , there is a daemon in linux called "CRONTAB" . Crontab : Crontab called as CRON TABle Where  cron is a daemon to run periodically on a given time and Table consists of scheduled tasks , jobs  i . e , Crons . Crontab Options:    crontab - l : lists all the crontabs scheduled . crontab - e : Edits existing cron or else create new cron. crontab - r : Removes scheduled cron jobs . crontab - i - r : Prompts before deleting user 's crontab i.e,asks your permission.  crontab -u username -l : lists all the crons of a particular user.    Setting up a cron: Press Ctrl+Alt+t to open the terminal ...