
<?php

session_start();
  
  $_SESSION['datos']  = $_POST['datos'];

$connection = new mysqli("localhost", "root", "admin") or die(mysqli_error());



$json9 = '{"registros":1,"item":[{"itemcode":"101","dep":"1","onhand":"6"}]}';


$books = json_decode( $_SESSION['datos'] ,TRUE);






//print_r($books);

$recibidos=$books['registros'];




$books2 =$books["item"];

foreach( $books2 as $book ) {
	


$sql ="insert into 171_calzashoes.prod_existencia (pr_id,p_existencia_dep_id,p_existencia) 

             values ('".$book["itemcode"]."','".$book["dep"]."','".$book["onhand"]."')
               ON DUPLICATE KEY UPDATE pr_id=values(pr_id),p_existencia_dep_id=values(p_existencia_dep_id),p_existencia=values(p_existencia)";
             
             
             
               $_SESSION['sql'] =$sql;


//logs( 'sql ' . $sql );

//. mysqli_error($sql)

$i=0;

if ($connection->query($sql)) {
$msg = array("status" =>1 , "msg" => "Your record inserted successfully");

$i=$i+1;
} else {
echo "Error al insertar: " . $sql . "<br>" . mysqli_error($connection);


}


  
}



$msg = array("estado" => "OK", "mensaje" => "Oper. enviadas en total: ".$recibidos." Procesados: ".$i);


$json = $msg;


@mysqli_close($connection);

header('content-type: application/json');
echo json_encode($json);


//print_r ($books2);
 //access property of object in array
//   foreach ($books2 as $book) {
 //   echo 'This book is a ' . $book->op_id . ' Autor:  ' . $book->op_fecha ."<br>";
//}


?>


