Hi All,
I have a bit of a problem I was hoping for some help. I have a script that takes a CSV file and adds fields to the database. What I need is instead of having to write the values over and over, just have the data go in without the headers. The fields (Columns) are static at this point and I want to see if I can just take a whole file and upload it to the database.
[php] //get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,“r”);
//loop through the csv file and insert into database
do {
if ($data[0]) {
$mysql_query = mysql_query("INSERT INTO carbon (VALUE1, VALUE2, VALUE3, VALUE4, VALUE5, VALUE6, VALUE7, VALUE8, VALUE9, VALUE10, VALUE11, VALUE12, VALUE13, VALUE14) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
'".addslashes($data[9])."',
'".addslashes($data[10])."',
'".addslashes($data[11])."',
'".addslashes($data[12])."',
'".addslashes($data[13])."'
)
");
}
} while ($data = fgetcsv($handle,10000,",","'"));
//
//redirect[/php]
Here is all of the Code I am using:
[php]<?php
include ‘_inc/include.php’;
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$mysql_query = mysql_query("INSERT INTO carbon (VALUE1, VALUE2, VALUE3, VALUE4, VALUE5, VALUE6, VALUE7, VALUE8, VALUE9, VALUE10, VALUE11, VALUE12, VALUE13, VALUE14) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
'".addslashes($data[9])."',
'".addslashes($data[10])."',
'".addslashes($data[11])."',
'".addslashes($data[12])."',
'".addslashes($data[13])."'
)
");
}
} while ($data = fgetcsv($handle,10000,",","'"));
//
//redirect
}
?>
Import a CSV File with PHP & MySQL <?php if (!empty($_GET[success])) { echo "Your file has been imported by import 55."; } //generic success notice ?> Choose your file:
Display Data Contents here
[/php]