<?php
session_start();
include_once '../classes/db.class.php';
include_once '../classes/fc.class.php';

	
if ($_SERVER['REQUEST_METHOD'] === 'POST'){

	//array_shift($_POST);

	
    $primaria = $_SESSION['primaria'];
    $tabela = $_SESSION['tabela'];
    $id=$_POST[$primaria];

    $sql = "UPDATE ".$tabela." SET ";

    $contador = 2;
    foreach (array_keys($_POST) as $indice) {
        
        if (count($_POST) > $contador && $indice != $primaria) {
            $sql .= "{$indice} = '{$_POST[$indice]}', ";
        } else if (count($_POST) <= $contador && $indice != $primaria) {
            $sql .= "{$indice} = '{$_POST[$indice]}' ";
        }
        $contador++;
    }

    $sql .= " WHERE ".$primaria."='$id'";
    
    $con=DB::conecta();
    $stmt = $con->prepare($sql);

    if($stmt->execute()){
	   // $con=null;
	    echo "ok";
	}else{
	    $con=null;
	    echo "erro";
	}
    
   //echo $sql;



}else{
	echo "erro";
}