Use the result of an array as the value of an insert - PHP

0

Good evening, I have a question.

I'm uploading a file and extracting its contents and inserting into my database, but I'm having a question about using the array as value

>

My file consists of a sequence of numbers separated by 15 blocks divided by a | and a break of lines between every 15 blocks

Ex. of the reading 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 insert into the database the information contained in this file so I created a table with the columns in names of a, b, c ...

If anyone can help me, I'm very grateful!

Att; Danilo Braga

    
asked by anonymous 04.03.2017 / 05:34

2 answers

0
  

/! \ This publication DOES NOT consider ANY security aspect!

This VALUES ( '".$array[$i]."' ") does not exist, at least not in this context, besides the error that exists in your query that does not have ) at the end.

You can do this as follows:

$valoresParaQuery = trim( trim( str_replace('|', "','", $linha) ) , "','");

$query = mysql_query("INSERT INTO dados (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) VALUES ( '" . $string . "') ");

The str_replace will replace all | with , and trim will remove any ',' that is before or after, like this:

INSERT INTO dados (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) VALUES ('02','01','020320171212','524419188','206','000011900173','03','700000','700000','644490','033438','2622221','C','M','C')

This still has error because there are only 15 values against 16 columns, to correct this REMOVE the column that is AUTO_INCREMENT , thus removing a .

But you say you have "a break of lines between every 15 blocks", if you want to insert all the lines do a loop per line, this is not being done in the current code .

Use for example:

include 'conect.php';

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0){

    $tmpName  = $_FILES['userfile']['tmp_name'];

    $arquivo = file($tmpName);
    // Veja se o arquivo está exatamente no local, o 'tmp_name' não informa o caminho do arquivo, normalmente na pasta /tmp/!

    foreach($arquivo as $linha){

        $valoresParaQuery = trim( trim( str_replace('|', "','", $linha) ) , "','");

        $query = mysql_query("INSERT INTO dados (b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) VALUES ('" . $valoresParaQuery . "') ");

    }

}

In this way each line will be done the process described above, changing the | by , and then executing the query.

I will not comment on security issues and vulnerabilities because they are too many

    
04.03.2017 / 06:17
0
$content= str_replace("|".Chr(10)."|", Chr(10), $content);
$content= str_replace(Chr(10)."|", Chr(10), $content);

    $array= explode(Chr(10),$content);

    $result = count($array);                

    for ($k = 0; $k < $result; $k++) {
        $bloco = $array[$k];
        $aDest = explode("|", $bloco);
        $query = mysql_query("INSERT INTO spedido (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) VALUES ('".$aDest[0]."','".$aDest[1]."','".$aDest[2]."','".$aDest[3]."','".$aDest[4]."','".$aDest[5]."','".$aDest[6]."','".$aDest[7]."','".$aDest[8]."','".$aDest[9]."','".$aDest[10]."','".$aDest[11]."','".$aDest[12]."','".$aDest[13]."','".$aDest[14]."') ");

It worked round in my bank with

$ content="01 | 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 | 03 | 01 | 020320171427 | 524418444 | 206 | 000011915070 | 01 | 31900 | 31900 | 31102 | 250802 | 2623485 | C | M | D ";

    
04.03.2017 / 17:49