Insert Mysql data with file . txt

Asked

Viewed 810 times

2

I have the following file, somewhat simple:

new_user.txt

JOAO|18|JOAO VITOR BARBOSA|MANDAQUI
ROGERIO|38|ROGERIO JOSE DE BARROS|CAPAO REDONDO

My table in the bank was created as follows:

users

create table users (
    primeiro_nome varchar(100),
    idade varchar(2),
    nome varchar(255),
    bairro varchar(255)
);

My PHP code is as follows:

<?php
require_once('../includes/conecta.php');

$file = fopen('new_user.txt', 'r'); 

while(!feof($file)){
    $data = explode('|', fgets($file)); 
    $query = "INSERT INTO users (primeiro_nome, idade, nome, bairro) VALUES ('".implode("', '", $data)."');";

    $executa = mysqli_query($conexao, $query);

    if($executa){
        echo 'DADOS INSERIDOS COM SUCESSO';
    } else {
        echo 'OCORREU UM ERRO!';
    }

}

When I give one echo in $query, he returns me the following:

INSERT INTO users (primeiro_nome, idade, nome, bairro) VALUES ('JOAO', '18', 'JOAO VITOR BARBOSA SOUZA', 'MANDAQUI ');INSERT INTO users (primeiro_nome, idade, nome, bairro) VALUES ('ROGERIO', '38', 'ROGERIO JOSE DE BARROS', 'CAPAO REDONDO');

If you take into account, apparently the queries are right, and in the validator I put, it returns the error and inserts nothing in the table.

The connection is working perfectly.

  • Haven’t you put in your question what mistake is coming to you

  • Precisely, it does not return me any error @Sorack

  • There are 2 Inserts in the same mysqli_query()?

  • Yeah, doesn’t it work that way? @rray

1 answer

3

The function mysqli_query() excuta only one SQL statement at a time, the semicolon makes the delimitation between the instructions so this generates a syntax error.

There are two ways to solve this the first and most practical is to send only one Insert with several keys VALUES, this approach ensures that all or no Serts are made.

$query = "INSERT INTO users (primeiro_nome, idade, nome, bairro) VALUES "; 
$values = trim("('".implode("','", $data)."'),", ',');
$executa = mysqli_query($conexao, $query) or die(mysqli_error($conexao));       

In place of:

INSERT INTO users (primeiro_nome, idade, nome, bairro) VALUES ('JOAO', '18', 'JOAO VITOR BARBOSA SOUZA', 'MANDAQUI ');

INSERT INTO users (primeiro_nome, idade, nome, bairro) VALUES ('ROGERIO', '38', 'ROGERIO JOSE DE BARROS', 'CAPAO REDONDO');

Will be generated:

INSERT INTO users (primeiro_nome, idade, nome, bairro) VALUES
('JOAO', '18', 'JOAO VITOR BARBOSA SOUZA', 'MANDAQUI '),
('ROGERIO', '38', 'ROGERIO JOSE DE BARROS', 'CAPAO REDONDO')

The other is to use function mysqli_multi_query()

  • me using the mysqli_multi_query() I can simply switch in my code?

  • @jvbarsou, yes (although I’m not a fan of multi_query()) I’m editing the answer with the code.

  • @jvbarsou edited the answer

  • just switching to the mysqli_multi_query It worked perfectly, because you’re not a fan of her? Alias, the second method, from the creation of other values did not work, as this inside the while, was copied several times to $query, then instead of adapting I went straight to the mysqli_multi_query and got results!

  • when I try to upload a file that contains more than 500 lines, it crashes and n ends the upload. some apparent reason?

  • @jvbarsou is testing on localhost or external u?

  • on a localhost

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.