Save multiple records in the bank

0

I'm using the following code to no avail.

$conn = mysqli_connect('localhost','user','pass','banco');
    try {

        $body = file_get_contents("php://input");

        $dados = json_decode($body);

        foreach($dados as $dataone){
          $nome = $dataone['nome'];
          $img = $dataone['img'];
          $qtd = $dataone['qtd'];        
        
            $sql = "INSERT into tabela (nome,img,qtd) values ('{$nome}','{$img}','{$qtd}')";
            
            $qr=mysqli_query($conn,$sql);        
      }
    
asked by anonymous 09.10.2017 / 01:52

3 answers

0

Next, I believe that first, the JSON file itself being sent to the php server contains errors, recheck the JSON code being sent and compare what I'm going to post. JSON file that should be sent to the PHP server:

{
    "pessoas": 
    [
        {   "nome": "Pedro", 
            "img": "images/pedro.png", 
            "qtd": "5"
        }, 
        {   "nome": "João",
            "img": "images/joao.png",
            "qtd": "8"
        }, 
        {   "nome": "Jose",
            "img": "images/jose.png",
            "qtd": "7"
        }
    ]
}

PHP script to insert data from the JSON file into the database:

<?php
    $conn = mysqli_connect("localhost","user","pass","banco");

        try {

            $body = file_get_contents("php://input");

            $dados = json_decode($body);

            for($i = 0; $i < count($dados->pessoas), $i++){

                $nome = $dados->pessoas[$i]->nome;
                $img = $dados->pessoas[$i]->img;
                $qtd = $dados->pessoas[$i]->qtd;

                $sql = "INSERT into tabela (nome,img,qtd) values ('$nome','$img','$qtd')";

                $query = mysqli_query($conn, $sql);

                if ($query){
                    echo "Dados inseridos no banco com Sucesso";
                } else {
                    echo "Houve um erro enquanto tentava inserir dados no banco : " . mysqli_connect_error($conn);
                }   
            }

          }
?>
    
09.10.2017 / 21:11
1

See if the script below works, if it does not work, note the error generated and post here in the comments:

<?php
    $conn = mysqli_connect("localhost","user","pass","banco"); // Se não funciona, tente com a linha abaixo
    //$conn = mysqli_connect("localhost","root","pass","banco");
        try {

            $body = file_get_contents("php://input");

            $dados = json_decode($body);

            foreach($dados as $dataone){

                $nome = $dataone['nome'];
                $img = $dataone['img'];
                $qtd = $dataone['qtd'];        

                $sql = "INSERT into tabela (nome,img,qtd) values ('$nome','$img','$qtd')";

                $qr = mysqli_query($conn, $sql);

                if ($qr){
                    echo "Sucesso";
                } else {
                    echo "[ERRO]: " . mysqli_connect_error($conn);
                }               
          }
?>
    
09.10.2017 / 02:10
0

Note: my table with latin1_general_ci

This is not my beach, but I did a test and I only succeeded by making these changes:

In the page I used:

<meta charset="utf-8" />

The code sent to php with the following syntax:

$body = '[{ "nome": "Pedro", "img": "images/pedro.png", "qtd": "5"}, { "nome": "João", "img": "images/joao.png", "qtd": "8"}, { "nome": "Jose", "img": "images/jose.png", "qtd": "7"}]';

The accented letters cause errors (John), I used utf8_encode .

utf8_encode - decodes a string in the ISO-8859-1 standard for the utf-8 standard.

$body = utf8_encode ($body);

and finally I used:

$dados = json_decode($body,true);
  

json_decode parses the JSON encoded string and converts it to a PHP variable, when TRUE, the return will be converted into an associative array.

    
09.10.2017 / 17:11