mysql - PHP - Import csv into database data too long -
i'm trying upload csv file save records database php. used sql method load data infile
didn't work anyway.
index.php has form <input name='csv' type='file' id='csv'/>
.
my file upload has 5 strings columns , 2 integers (last of them), has 2 rows, header , values.
they fields not null
in 'usuarios' database. here's problem, when trying add record (for instance: 'bea') says that
.....(sooo long)......k8docprops/app.xmlpk data long column 'nombreusuario' @ row 1
yeah, readfile shows that, changed details of every column (i don't think problem) , put values varchar(200) / integer(200)
, whatever doesn't let me put more length because tried specified key long; max key length 767 bytes
.
and here's code, made others examples:
subircsv.php
require ('../cabses.php'); require ('../conecta.php'); if (isset($_post['submit'])) { if (is_uploaded_file($_files['csv']['tmp_name'])) { echo "file ". $_files['csv']['name'] ." uploaded successfully."; echo "displaying contents:"; readfile($_files['csv']['tmp_name']); } $handle = fopen($_files['csv']['tmp_name'], "r"); $flag = true; while (($data = fgetcsv($handle, 1000, " ")) !== false) { if($flag) { $flag = false; continue; } $import="insert usuarios (nombreusuario,passusuario,emailusuario,nombre,apellidos,idpropietario,idrol) values ( '".trim($data[0], '"')."', '".trim($data[1], '"')."', '".trim($data[2], '"')."', '".trim($data[3], '"')."', '".trim($data[4], '"')."', '".trim($data[5], '"')."', '".trim($data[6], '"')."' ) "; $oconni->query($import) or die(mysqli_error($oconni)."____________".$import); } fclose($handle); print "import done"; } else { print "not working"; }
maybe utf-8 encode?
this first question in stackoverflow, hello spain! , thank you!
well, finished it! first @ realized separator ;
, addslashes($data[0])
works fine.
you can use code , try it.
require ('../conecta.php'); if (isset($_post['submit'])) { if (is_uploaded_file($_files['csv']['tmp_name'])) { $handle = fopen($_files['csv']['tmp_name'], "r"); $flag = true; while (($data = fgetcsv($handle, 1000, ";")) !== false) { if($flag) { $flag = false; continue; } $sql="insert usuarios (nombreusuario,passusuario,emailusuario,nombre,apellidos,idpropietario,idrol) values ( '".addslashes($data[0])."', '".addslashes(md5($data[1]))."', '".addslashes($data[2])."', '".addslashes($data[3])."', '".addslashes($data[4])."', '".addslashes($data[5])."', '".addslashes($data[6])."' ) "; $oconni->query($sql); } fclose($handle); header('location:../index.php'); } else { print "no funciona"; } }
Comments
Post a Comment