i want to insert data from a csv file into my mysql database with php. But i dont know what i doing wrong.
This is my php code
if ($_FILES[csv][size] > 0){
$csv_file = $_FILES[csv][tmp_name]; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile)) {
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['id'] = $csv_array[0];
$insert_csv['name'] = $csv_array[1];
$insert_csv['email'] = $csv_array[2];
if(!empty($insert_csv['email'])){
$query = "INSERT INTO contacts(id,name,email)
VALUES('','".$insert_csv['name']."','".$insert_csv['email']."')";
$n=mysqli_query($database->connection,$query);
}
$i++;
}
fclose($csvfile);
}
This is my csv looks like.
id---- name ------- email
1 ---- user1--------bla#hotmail.com
2 ---- user2 --------blah
3------ user 3 ------ blah
When i run this code my mysql results are
in email table = ##0.00 "TL")$# en in my name table= also ##0.00
"TL")$#;
What do i wrong?
You might want to use MySQL to do the whole loading process with the LOAD DATA INFILE statement.
if($_FILES['csv']['error'] === UPLOAD_ERR_OK && $_FILES['csv']['size'] > 0) {
$query = "LOAD DATA INFILE '" . $_FILES['csv']['tmp_name']
. "' INTO TABLE contacts FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' (id, name, email);";
if(!mysqli_query($query)){
die('Oops! Something went wrong!');
}
}
If required you can tweak the loading parameters (FIELDS TERMINATED BY, ENCLOSED BY, LINES TERMINATED BY).
Do take note that if you use this approach your temporary file needs to be stored in a place where its accessible by the MySQL server (like /tmp).
To start with, I think you should remove the first
$data = fgetcsv($getfile, 1000, ",");
line, outside of the while loop...
Please try like this as a example , it should work for you as you want
I think you missed qoutes in "
$query = "INSERT INTO contacts(id,name,email)
VALUES('".$col1."','".$col2."','".$col3."')";
"
<?php
$csv_file = 'C:\wamp\www\stockmarket\test.csv'; // Name of your CSV file with path
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
$data = fgetcsv($getfile, 1000, ",");
while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$result = $data;
$str = implode(",", $result);
$slice = explode(",", $str);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
// SQL Query to insert data into DataBase
$query = "INSERT INTO contacts(id,name,email)
VALUES('".$col1."','".$col2."','".$col3."')";
$s=mysql_query($query, $connect );
}
}
}
?>
Related
I know this has been asked before -- I tried to read previous Q&A on the topic but I'm still stuck. Probably I read too many Q&A and have a bad mix of techniques as a result.
I don't get an error, I just don't get anything in my table. The echo $i is to help debug and I only ever get a 0 rather than expected 0 1 2 3 ... N rows.
My DB connection credentials are all fine I use them all over my site for Select statements.
$csv_file = str_getcsv("https://ds.data.jma.go.jp/tcc/tcc/products/gwp/temp/list/csv/year_wld.csv");
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
echo $i;
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$yrr = $csv_array[0];
$vals= $csv_array[1];
$sql1 = "INSERT INTO Table1(Year,Value) VALUES(" . $yrr . "," . $vals. ")";
$conn->query($sql1);
$i++;
}
The main problem here is the fact that you are trying to open a text variable as a file:
$csvfile = fopen($csv_file, 'r');
In fact you already have an array from from str_getcsv so your whole code should look like (if you can read the whole file at once):
$csvFile = array_map('str_getcsv', file("https://ds.data.jma.go.jp/tcc/tcc/products/gwp/temp/list/csv/year_wld.csv"));
array_shift($csvFile); //we remove the headers
$i = 0;
/**
* Removes all the "*" and "+" symbols as I assume that you want a float since you are not wrapping it in the sql query
*/
function removeUnwantedChars($string) {
return preg_replace('/[^0-9\\.\\-]/i', '', $string);
}
foreach($csvFile as $csvData) {
echo $i++;
$yrr = $csvData[0];
$vals = removeUnwantedChars($csvData[1]);
$sql1 = "INSERT INTO Table1(Year,Value) VALUES(" . $yrr . "," . $vals. ")";
$conn->query($sql1);
}
If you cannot read it all at once then I suggest to first download the file line by line:
<?php
$url = "https://ds.data.jma.go.jp/tcc/tcc/products/gwp/temp/list/csv/year_wld.csv";
$fileHandle = fopen($url, "r");
/**
* Removes all the "*" and "+" symbols
*/
function removeUnwantedChars($string) {
return preg_replace('/[^0-9\\.\\-]/i', '', $string);
}
$i = 0;
$headersSkipped = false;
while ($csvData = fgetcsv($fileHandle)) {
if (!$headersSkipped) {
$headersSkipped = true;
continue;
}
echo $i++;
$yrr = $csvData[0];
$vals = removeUnwantedChars($csvData[1]);
$sql1 = "INSERT INTO Table1(Year,Value) VALUES(" . $yrr . "," . $vals. ")";
$conn->query($sql1);
}
fclose($fileHandle);
Yet like said by #Shadow above it is always great to be more verbose. So in case query returned false then it would be great to output the last error (if you are using PDO then errorInfo() function.
I am trying to import a csv file into the data base without defining any of the rows as it will be automatic when the page loads -
$file = '../csv/file.csv';
$table = 'table_name';
// get structure from csv and insert db
ini_set('auto_detect_line_endings',TRUE);
$handle = fopen($file,'r');
// first row, structure
if ( ($data = fgetcsv($handle) ) === FALSE ) {
echo "Cannot read from csv $file";die();
}
$fields = array();
$field_count = 0;
for($i=0;$i<count($data); $i++) {
$f = strtolower(trim($data[$i]));
if ($f) {
// normalize the field name, strip to 20 chars if too long
$f = substr(preg_replace ('/[^0-9a-z]/', '_', $f), 0, 255);
$field_count++;
$fields[] = $f.' VARCHAR(255)';
}
}
$sql = "CREATE TABLE $table (" . implode(', ', $fields) . ')';
$conn->query($sql);
while ( ($data = fgetcsv($handle) ) !== FALSE ) {
$fields = array();
for($i=0;$i<$field_count; $i++) {
$fields[] = '\''.addslashes($data[$i]).'\'';
}
$sql = "Insert into $table values(" . implode(', ', $fields) . ')';
$conn->query($sql);
}
fclose($handle);
ini_set('auto_detect_line_endings',FALSE);
It loads the data in the page if I echo it and it creates the table correctly just doesn't load the data into the table and I cant find out why..
Thanks!
UPDATED
Here is the first row I am getting when I echo $sql -
Insert into 1001_inventory values('New', '581613', '88888888888888888', '2016', 'Toyota')
So I found that the varchar(255) was to small for some of the fields so I expanded it and the problem is solved, Thanks for all the help!
Read first row from csv file and create table automatically according to it(csv file fields) in mysql. Looking for PHP script?
I have tried this
<?php
$arr = array(array(),array());
$num = 0;
$row = 0;
$handle = fopen("./contacts.csv", "r");
while($data = fgetcsv($handle,1000,",")){
$num = count($data);
for ($c=0; $c < $num; $c++) {
$arr[$row][$c] = $data[$c];
}
$row++;
}
$con = mysql_connect('localhost','root','');
mysql_select_db("excel_database",$con);
for($i=1; $i<$row; $i++){
$sql = "INSERT INTO contacts VALUES ('".$arr[$i][0]."','".$arr[$i][1]."','".$arr[$i][2]."','".$arr[$i][3]."','".$arr[$i][4]."','".$arr[$i][5]."')";
mysql_query($sql,$con);
}
?>
This is half way to your question.
<?php
$row = 1;
if (($handle = fopen("ab.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
whith array_shift(file('filename.csv')) will give you the first line of the document filename.csv
Modifiying your script a bit, this should do the trick and create the table according to the csv header line:
$sql[] = array();
$handle = fopen($file, "r");
$header = fgetcsv($handle,1000,",");
if($header){
$header_sql = array();
foreach($header as $h){
$header_sql[] = '`'.$h.'` VARCHAR(255)';
}
$sql[] = 'CREATE TABLE `imported_csv` ('.implode(',',$header_sql).')';
while($data = fgetcsv($handle,1000,",")){
$sql[] = "INSERT INTO imported_csv VALUES ('".implode("','",$data)."')";
}
}
$con = mysql_connect('localhost','root','');
mysql_select_db("excel_database",$con);
foreach($sql as $s){
mysql_query($s,$con);
}
Here's a single function to give you an array: fgetcsv See Example #1 on that page for a basic script.
The table will just be a matter of looping over the contents ...
Aha, I finally parsed "table automatically according to it in mysql" and understood. Sorry. fgetcsv will return an array; just use that array to create your sql command. e.g. '...values (' . implode(',', $results_of_fgetcsv) . ')...'
If you want to visualize the table data, in html, you can create a table using PHP and data from csv reading the first line as headers. I use this function that you can try it!
function build_table($source){
$data = array();
$file = fopen($source, 'r');
while (($result = fgetcsv($file,0,";")) !== false){$data[] = $result;}
fclose($file);
//Now process the data and create table
$data_rows=#count($data[0]); //from the first row get the header name
$colum_trail="";
//Create the header part
for($i=0; $i<$data_rows;$i++){
$cell=$data[0][$i]; //each header
$colum_trail.="<th>$cell</th>";
}
////Create the table body contents
$table_cells="";
for($i=1; $i<count($data);$i++){
$table_cells.="<tr>";
$cell=$data[$i];
foreach($cell as $cell_value){
$cell_v=$cell_value;
$table_cells.="<td>$cell_v</td>";
}
$table_cells.="</tr>";
}
$table="<table><tr>$colum_trail</tr>$table_cells</table>";
echo "<style>td {border-bottom: 2px solid black;}</style>";
echo $table;
}
Then all you need is to after that code, call that function like this:
build_table("source.csv"); //then call the file you want to create table
I here have a codes that inputs excel into my table Biometrics. Its working but i cant find a way to import it to other table which has only 3 fields. in my CSV I have 5 columns. I only want to get in my CSV 1 column and put it in my User_dummy table can you help me with
`
mysql_select_db("hris_db",$link) or die ("Cannot select the database!");
// Set your CSV feed
$feed = 'excel/SampleLogs.csv';
//$uploadid = 2;
// Arrays we'll use later
$keys = array();
$newArray = array();
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 0, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
// Do it
$data = csvToArray($feed, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($data);
//Use first row for names
$labels = array('empno','date_created','time_created','status','device');
foreach ($labels as $label) {
$keys[] = trim($label);
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = array_map('trim',$d);
}
//count number of rows in database
$q = "SELECT * FROM biometrics";
$res = mysql_query($q);
$numrows = mysql_num_rows($res);
$slicearray = array_slice($newArray,$numrows);
echo $numrows;
var_dump($slicearray);
//$reverse = array_reverse($newArray,true);
//var_dump($reverse);
//$uniqueid = uniqid();
foreach($slicearray as $key=>$value){
$implodearray = "'" . implode($value, "','") . "'";
$keysString = implode(",", array_keys($value));
$keylower = strtolower(str_replace(str_split(" '-/"),'_',$keysString));
$sql = "INSERT INTO biometrics ($keylower)
SELECT * FROM (SELECT '".$value['empno']."','".$value['date_created']."','".$value['time_created']."','".$value['status']."' as status,'".$value['device']."' as device) As tmp
WHERE NOT EXISTS (SELECT $keylower FROM biometrics WHERE empno = '".$value['empno']."' AND date_created = '".$value['date_created']."' AND time_created = '".$value['time_created']."' AND status = '".$value['status']."' AND device = '".$value['device']."')";
mysql_query($sql) or die(mysql_error());
//echo $sql;
//var_dump($keylower);
//var_dump($value);
}
?>`
yes this is very simple
http://imgur.com/pZTzVbj
use this link image and you get idea how to import csv in database
or if you want to this by query then use it
LOAD DATA LOCAL INFILE '/your_file.csv'
INTO TABLE your_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY ','
(field1, filed2, field3);
Following up on my last thread. Trying to import a user-generated CSV into MySQL via a PHP upload script. Uploads successfully, but I am not able to use LOAD DATA due to a permissions problem. Here is what I am trying to do instead:
$row = 1;
if (($handle = fopen($target_path, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++)
{
$fullRow = $fullRow . $data[$c] . "," ;
}
echo $fullRow;
mysql_query("INSERT INTO foo (field1, field2, field3, field4) VALUES ('$fullRow')");
$fullRow = NULL;
}
fclose($handle);
}
echo $fullRow spits out a verbatim copy of the line from the CSV file, except for an additional comma on the end. Is this why the Insert is not working correctly? When I do a manual upload via phpMyAdmin, the CSV file is imported without issue. Or is there a problem with the VALUE ('$fullRow') bit of the code?
You can simply remove the last comma.
for ($c=0; $c < $num; $c++)
{
$fullRow = $fullRow . $data[$c] . "," ;
}
echo $fullRow;
$fullRow = substr($fullRow,0,-1);
And also you script is not ok.
mysql_query(" INSERT INTO foo (field1, field2, field3, field4) VALUES ('$fullRow') " );
$fullRow = NULL;
Paolo_NL_FR's fixes should get you up and running. The script could use some TLC though, and does not have even basic sql injection protection. Try something like this perhaps:
if (($handle = fopen($target_path, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$values = implode(",", array_map('mysql_real_escape_string', $data));
mysql_query("INSERT INTO foo (field1, field2, field3, field4) VALUES ($values);");
}
fclose($handle);
}