0

I'm trying to sync my Android local database to my webservice, but I'm getting some problems inserting the JSON string into mysql, because all my values are null. From Android I send the JSON string and this is what I get in Php.

  [{
        "prop_id": "1",
        "edo_sync": "No",
        "titulo": "Departamento en renta ",
        "actualizacion": "2016:10:25 19:14:33",
        "categoria": "Venta",
        "superficie": "30m2",
        "_id": "1",
        "propiedad": "Casa",
        "fecha": "10/25/16"
    }, {
        "prop_id": "1",
        "edo_sync": "No",
        "titulo": "Casa en renta",
        "actualizacion": "2016:10:27 13:30:08",
        "categoria": "Venta",
        "superficie": "100m2",
        "_id": "2",
        "propiedad": "Casa",
        "fecha": "10/25/16"
    }, etc...
    }]

this is my php file:

<?php
try
{
    include("constantes.php");
    //Recibimos Objeto Json
    $Propiedad = $_POST["PropiedadPost"];

    if (!get_magic_quotes_gpc()) {
        echo "activado";
    $Propiedad = stripslashes($Propiedad);


    //Decode JSON into an Array
    $data = json_decode($Propiedad );

    print_r ($data);

    $options  = array('uri' => URL_BASE,'location' => WEBSERVICE);
    $client   = new SoapClient(NULL, $options); 

    for($i=0; $i<count($data) ; $i++)
    {
    //Store prop into MySQL DB
        $response = $client->InsertarPropiedad($data[$i]);
        print_r ($data[$i]);
        echo $response;
    }   
}
    }catch(Exception  $e){
        echo "Error:".$e->getMessage();
    }
?>

and here is my class to insert data.

public function InsertarPropiedad($CadenaJson) {
    $ObjetoJson = json_decode($CadenaJson);
    $response   = array("Msg"=>"","IdRegistro"=>"0"); 
    try
    { 
        $stmt = $this->db->prepare("INSERT INTO PROPIEDADES (prop_id,titulo,propiedad,categoria,superficie,fecha,actualizacion,edo_sync,_id) VALUES(:PROP_ID,:TITULO,:PROPIEDAD,:CATEGORIA,:SUPERFICIE,:FECHA,:ACTUALIZACION,:EDO_SYNC.:ID)");
        $stmt->bindparam(":PROP_ID",$ObjetoJson->prop_id);
        $stmt->bindparam(":TITULO",$ObjetoJson->titulo);
        $stmt->bindparam(":PROPIEDAD",$ObjetoJson->propiedad);
        $stmt->bindparam(":CATEGORIA",$ObjetoJson->categoria);
        $stmt->bindparam(":SUPERFICIE",$ObjetoJson->superficie);
        $stmt->bindparam(":FECHA",$ObjetoJson->fecha);
        $stmt->bindparam(":ACTUALIZACION",$ObjetoJson->actualizacion);
        $stmt->bindparam(":EDO_SYNC",$ObjetoJson->edo_sync);
        $stmt->bindparam(":ID",$ObjetoJson->_id);

        $stmt->execute();

        $response["Msg"] = "Propiedad Registrado Correctamente";
        $response["IdRegistro"] = $this->db->lastInsertId(); 
    }
    catch(PDOException $e)
    {
        $response["Msg"]       = $e->getMessage();
        $response["IdRegistro"] = "0"; 
    }
    return json_encode($response);
}

the message i get:

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'prop_id' cannot be null"

0 Answers0