I have a json
that I want to travel through ajax
to a file php
, and load my database into it, passing each item in the file json
as a new table row.
ajax:
var data = { 'data': JSON.stringify(arrayEventos) }
$.ajax({
type: 'POST',
url: 'ajax/turnos.ajax.php',
dataType: 'json',
data: data,
success: function(data, status, xhr) {
alert("response was " + data);
},
error: function(xhr, status, errorMessage) {
$("#debug").append("RESPONSE: " + xhr.responseText + ", error: " + errorMessage);
}
});
value of what I am passing by ajax
data = [{"title":"Agustin Guerra","start":"2020-03-13T09:00:00","end":"2020-03-13T10:00:00"},{"title":"Mariel Guerrieri","start":"2020-03-13T11:30:00","end":"2020-03-13T12:30:00"}]
php where I receive the variable data
<?php
require_once "../modelos/turnos.modelo.php";
class AjaxTurnos{
public $data;
public function ajaxActualizarTurnos(){
$datos = $this->data;
$respuesta = ModeloTurnos::mdlActualizarTurnos($datos);
return $respuesta;
}
}
/*==============================
ACTUALIZAR TURNOS
==============================*/
if(isset($_POST["data"])){
$turnos = new AjaxTurnos();
$turnos -> data = $_POST["data"];
$turnos -> ajaxActualizarTurnos();
}
php that saves to the database
static public function mdlActualizarTurnos($datos){
$tabla = "turnos";
$stmt1 = Conexion::conectar()->prepare("TRUNCATE $tabla");
$stmt1->execute();
$stmt = Conexion::conectar()->prepare("INSERT INTO $tabla(datos) VALUES (:datos)");
$stmt->bindParam(":datos", $datos, PDO::PARAM_STR);
if($stmt->execute()){
return "ok";
}else{
return "error";
}
$stmt ->close();
$stmt -> null;
}
What happens to me is that ALL the content of the data variable is saved in the database in a single row:
ID:1 | DATOS: [{"title":"Agustin Guerra","start":"2020-03-13T09:00:00","end":"2020-03-13T10:00:00"},{"title":"Mariel Guerrieri","start":"2020-03-13T11:30:00","end":"2020-03-13T12:30:00"}]
When what I need is to save it as follows:
ID:1 | DATOS: {"title":"Agustin Guerra","start":"2020-03-13T09:00:00","end":"2020-03-13T10:00:00"}
ID:2 | DATOS: {"title":"Mariel Guerrieri","start":"2020-03-13T11:30:00","end":"2020-03-13T12:30:00"}
I tried putting a foreach in it
ajax.php
$datos = $this->data;
foreach($datos as $key => $value){
$respuesta = ModeloTurnos::mdlActualizarTurnos($value);
}
but i didn't succeed, any suggestion is welcome.
You are receiving an array of JSON objects . In JSON array is everything that starts with
[
and ends with]
, while object is everything that starts with{
and ends with}
.Seeing the structure with which you are going to work, you need:
$this->data
to JSON so you can iterate through it as an array of JSON objects.json_encode
to insert it.Something like that:
proof of concept
Departure:
Observations
I see that you make too many calls to
conectar()
maybe that code needs to be seriously optimized if in each of those calls you create connection objects withnew PDO (....)
.It means that for each row you insert you would be creating a connection , which would be too expensive. For this reason, the connection should never be called from a helper method that should be used to insert, select, delete... but not to connect. The connection should exist at a higher level in your class.