0

I am newbie in SQL and Excel is anyone can guide me for this?

This is my full script.

<?php
include 'function/functions.php';
$excel = $_POST['files'];
$upload = $_POST['upload'];
$query = mysql_query("INSERT INTO clients (NAME, AGE, SEX, ADDRESS, CONTACT_NUMBER) VALUES ($excel)");
if(isset($upload))
{
$query;
}
?>
<!doctype HTML>
<html>
<head>
</head>
<body>
<form action="" method="POST" />
Upload Excel File<br />
<input type="file" name="files" /><br />
<input type="submit" value="upload" name="upload" />
</form>
</body>
</html>

i'll make simple code to make easy to understand for me,(anyway sorry for my bad english)

my question is, How i can insert each corresponding column in excel in MySQL here's the picture.

enter image description here

enter image description here

2 Answers2

0

Use fgetcsv to get contents from excel file as array values.Hope it works for You.

 if($_FILES["file"]["size"] > 0)
     {

      $file = fopen($filename, "r");
             while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
             {
                $sql = "INSERT into import(NAME,AGE,SEX,ADDRESS,CONTACT_NUMBER) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]')";
                mysql_query($sql);
             }
             fclose($file);
             echo "CSV File has been successfully Imported";
     }
     else
     echo "Invalid File:Please Upload CSV File";
Ramkumar P
  • 196
  • 3
  • 13
0

You probably need some kind of Excel interpreter. I don't believe PHP comes with a default one.

This question seems to have been answered here: Reading an Excel file in PHP

I use PHP-ExcelReader to read xls files, and works great.

It seems to be a standalone library that you can include.

I would try that and then formulate a MySQL string to then insert into your database.

Community
  • 1
  • 1
Chad Fisher
  • 353
  • 4
  • 16