Send Array to DB

1

Good afternoon, guys. I'm trying to pass an array that I retrieve from a JSON to DB, but I'm not able to identify Array objects, I think I'm doing something wrong, I've done some tests with foreach but I did not succeed, follow the cod's.

HTML

<?php include('header.php'); ?>
<script type="text/javascript" src="script/script.js"></script>

<title>DataTable</title>

<style>
tfoot input {
width: 100%;
padding: 0;
box-sizing: border-box;
color: #FFFFFF;
}
tfoot {
display: table-footer-group;
}
</style>

<?php include('container.php'); ?>
<div class="container">
<h2>DataTable</h2>
<div class="row">
<table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>ID</th>
      <th>Data</th>
      <th>Horário</th>
      <th>Nº Telefone</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th></th>
      <th class="selectedDate" id="1">Data</th>
      <th></th>
      <th></th>
    </tr>
  </tfoot>
  </table>
  </div>
  </div>
  <?php include('footer.php'); ?>

script.js

// _START_ DataTable
var table = $('#example').DataTable({
"bProcessing": true,
"sAjaxSource": "data.php",
"bPaginate": true,
"sPaginationType": "full_numbers",
"iDisplayLength": 5,
"autoWidth": true,
"aoColumns": [
  {mData: [0]},
  {type: 'date-br',
  targets: 1},
  { "orderable": false, "targets": 2 }, // remove a ordenação
  { "orderable": false, "targets": 3 },
  ]
  });

// função para retornar o dados da tela em json
setInterval(function () {
var table3 = $('#example').tableToJSON();
var request =  $.ajax({
  method: "POST",
  url: "teste.php",
  data: { array: table3},
  dataType: "html"
})    
request.done(function(resposta) {
  //resposta servidor
  console.log(resposta)
});
}, 6000); // está em segundos

test.php

<?php
$array = ($_POST['array']);
print_r($array);

Array

Array
(
[0] => Array
    (
        [ID] => 
        [Data] => 
        [Horário] => 
        [Nº Telefone] => 
    )
[1] => Array
    (
        [ID] => 8010
        [Data] => 27/12/2017
        [Horário] => 14:58:27
        [Nº Telefone] => 1231530337
    )
[2] => Array
    (
        [ID] => 8010
        [Data] => 27/12/2017
        [Horário] => 14:56:52
        [Nº Telefone] => 1231530337
    )
)
    
asked by anonymous 27.12.2017 / 19:04

1 answer

0

PHP test.php

I tested the array as shown in the code below, and it is working round.

$db = pg_connect("host=localhost port=5432 dbname=call_lost user=diogom password=morfeu00");

    $array = (

    Array
    (
    "0" => Array
        (
            "ID" => "",
            "Data" => "",
            "Horário" => "",
            "Nº Telefone" => ""
        ),
    "1" => Array
        (
            "ID" => "8020",
            "Data" => "27/12/2017",
            "Horário" => "14:58:27",
            "Nº Telefone" => "1231530337"
        ),
    "2" => Array
        (
            "ID" => "8020",
            "Data" => "27/12/2017",
            "Horário" => "17:52:52",
            "Nº Telefone" => "1231530337"
        ),
    )

);


$values = array();
// construção dos values a serem inseridos na declaração insert
foreach ($array as $rowValues) {
  //se algum campo de um bloco for nulo, esse bloco não será salvo no banco
  $valido = false === array_search(false , $rowValues, false);
  if ($valido == true){
    $values[] = "(" ."'". implode('\',\' ', $rowValues) . "')";
  }
}
//declaração insert
$sql = "INSERT INTO ligacoes (cod,data,hora,telefone) VALUES " . implode (', ', $values);
$result = pg_query($sql); 
  

I adapted the array as shown in your question

    
27.12.2017 / 22:08