I need to separate strings to be able to import into mysql [closed]

0

I need to separate each string so I can import it into the database, how would I do it?

link of the file which I am testing link

            <?php 

            include_once("../conn/conexao.php");

            if ($_SERVER["REQUEST_METHOD"] === "POST") {

                $filename = $_FILES["file1"];

                if ($filename["error"]) {
                    throw new Exception("Error: ".$filename["error"]);
                }

                $dirUploads = "../uploads";

                if (move_uploaded_file($filename["tmp_name"], $dirUploads . DIRECTORY_SEPARATOR . $filename["name"])) {
                    echo "Upload Realizado com sucesso";
                }else {

                    throw new Exception("Não foi possivel fazer o upload.");


                }
            }

            $filename = $dirUploads . DIRECTORY_SEPARATOR . 
            $filename["name"];


            if(file_exists($filename)) {

                $file = fopen($filename, "r");

                $headers = explode(",", fgets($file));

                $data = array();

                while ($row = utf8_encode(fgets($file))) {

                    $rowData = explode(",", $row);
                    $linha = array();

                    for ($i = 0; $i < count($headers); $i++) {

                        $linha[$headers[$i]] = $rowData[$i];
                    }

                    array_push($data, $linha);

                    //$query1 = mysqli_query($conn, "INSERT 'clientes' SET nome='$row[1]', responsavel = '', endereco = '$row[4]', endereco_entrega = '', cpf = '$row[12]', telefone = '', cidade_estado = '', ie = '', cep = ''") or print mysql_error();

                }
                echo "<br>";
                /*if ($query1) {

                    echo "Tudo certo";
                }else {
                    echo "erro";
                }
                echo "<br>";*/




                fclose($file);


                //echo json_encode($data);
                //var_dump($data);
            }

             ?>
    
asked by anonymous 03.01.2018 / 19:09

1 answer

1

You are using the explode function and trying to separate the string with a comma, however, the CSV is using a semicolon ( ; ) as a separator.

Another detail, you can use fgetcsv . Just set the third parameter as a semicolon.

$headers = fgetcsv($file , 0 , ';');
    
04.01.2018 / 10:59