Some body tell me why does not work this expression? $sql = "INSERT into tabla(campo1, campo2, campo3, campo4) values('$data[0]','$data[1]', "","")"; PHP: Thanks.
typo: 'tabla', needs to be the actual table name not a misspelled keyword I suspect. If that's not it, try adding the database name dot firt (i.e. [mydb].tabla) Example: INSERT INTO tbl_group_users (user_name, group_name, group_member, group_admin) VALUES ('blah', 'blah', 'blah', true)
Also, if you're using a sane DB handler, and showing/logging errors, you'll be able to see exactly what the error is, and fix it.
This is the error message: this value is in data[1]. I need to escape values but how? <?php //conexiones, conexiones everywhere ini_set('display_errors', 1); error_reporting(E_ALL); $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $database = 'eaton'; $table = 'order'; if (!mysql_connect($db_host, $db_user, $db_pass)) die("No se pudo establecer conexión a la base de datos"); if (!mysql_select_db($database)) die("base de datos no existe"); if(isset($_POST['submit'])) { //Aquí es donde seleccionamos nuestro csv $fname = $_FILES['sel_file']['name']; echo 'Cargando nombre del archivo: '.$fname.' '; $chk_ext = explode(".",$fname); if(strtolower(end($chk_ext)) == "csv") { //si es correcto, entonces damos permisos de lectura para subir $filename = $_FILES['sel_file']['tmp_name']; $handle = fopen($filename, "r"); while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) { //Insertamos los datos con los valores... $sql = "INSERT into table (field1, field2, field3, field4) values('$data[0]','$data[1]','$data[2]','$data[3]')"; mysql_query($sql) or die(mysql_error()); } //cerramos la lectura del archivo "abrir archivo" con un "cerrar archivo" fclose($handle); echo "Importación exitosa!"; } else { //si aparece esto es posible que el archivo no tenga el formato adecuado, inclusive cuando es cvs, revisarlo para //ver si esta separado por " , " echo "Archivo invalido!"; } } ?> PHP: This code is for upload csv file and insert into database.
The immediate problem is that you haven't put curly brackets around the arrays so $sql = "INSERT into table (field1, field2, field3, field4) values('$data[0]','$data[1]','$data[2]','$data[3]')"; Code (markup): will break but this will work $sql = "INSERT into actualtablename (field1, field2, field3, field4) values('{$data[0]}','{$data[1]}','{$data[2]}','{$data[3]}')"; Code (markup): and this is even better $sql = "INSERT into `actualtablename` (`field1`, `field2`, `field3`, `field4`) values('{$data[0]}','{$data[1]}','{$data[2]}','{$data[3]}')"; Code (markup):
And none of them are good. You DO NOT USE mysql_ EVER. If you'd used mysqli_ eller PDO, a prepared statement would've fixed this error before it even was an issue.