0
Good night, I’m having a doubt.
I am uploading a file and extracting its contents and inserting in my database, but I have a question how to use the array like the value
My file consists of a sequence of numbers separated by 15 block divided by one | and a line break between every 15 blocks
Ex. of Read File 02|01|020320171212|524419188|206|000011900173|03|700000|700000|644490|033438|2622221|C|M|C| |02|01|020320171427|524418372|206|000011915070|01|31900|31900|31102|250802|2623485|C|M|D|
Ex. of my code upload.php
<?php
include 'conect.php';
///RECEBE PELO METODO POST
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$tmpName = $_FILES['userfile']['tmp_name'];
///ABRE O ARQUIVO
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
///SEPARA OS BLOCOS
$array=explode("|",$content);
for($i = 0; $i < count($array); $i++) {
$query = mysql_query("INSERT INTO dados (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) VALUES ( '".$array[$i]."' ");
/// Está dando o erro de Query was empty
mysql_query($query) or die(mysql_error());
}
if(mysql_affected_rows() != 0){
echo "<script type='text/javascript'>window.alert('$fileName Enviado!');</script>";
echo '<meta HTTP-EQUIV="Refresh" CONTENT="1; URL=lista.php">';
exit;
}
}
?>
I want to enter in the database the information contained in this file so I created a table with columns in names of a,b,c...
If any of you can help me, I’d be grateful!
Att. Danilo Braga
Good morning @Inkeliz thanks for the help, but the bug still persists. Corrected the issue of
)
at the end ofmysql_querry
and the fact that there are 16 columns for 15 elements is that it had not customized the names of the columns, wherea
would be the column with nameID
and she will be AUTO_INCREMENT I’ll use the NULL as the first element ofVALUE
. At first I do not want to save the file in any directory, but do a temporary reading, so I did not indicate the file path. I am using this last code sent, plus the error ofQuery was empty
still persists.– Danillo Braga
I fixed an error, but possibly your problem is totally different. Check if one
var_dump(file($tmpName))
returns the file. I have tested exactly the above code using Mysqli using PHP 7.1, I am making it available at https://www.dropbox.com/s/v5cc2lz1uwwzag5/187952.zip?dl=0. If it is "AUTO_INCREMENT" do not need to mention it.– Inkeliz