<?php
include "conn.php";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Make MySQLi throw exceptions.

        try {
            // Try Connect to the DB with new MySqli object - Params {hostname, userid, password, dbname}
            $mysqli = new mysqli($host, $user, $pass, $base);


            $tableData = stripcslashes($_POST['pTableData']);
            $tableData = json_decode($tableData,TRUE);
        
            foreach($tableData as $row => $ren) {
                /*
                foreach($ren as $key => $val) {
                    echo "$key = $val <br>";
                }
                */
                if($row==0){
                    $genero=$ren["tienda"];
                    $linea=$ren["almacen"];
                } else {
                    $query = "update metas set meta='" . $ren["meta"] . "' where tienda='" . $ren["tienda"] . "' and almacen='". $ren["almacen"] ."' and genero='".$genero."' and linea='".$linea."'";
                    //$query = "update metas set meta='" . $ren["meta"] . "' where tienda='" . $ren["tienda"] . "' and almacen='". $ren["almacen"] ."'";
                    //echo $query."<br>";
                    $mysqli->query($query);
                }
            }

            // regenera tablas metas2
            $query = "call sp_actualiza_metas()";
            $mysqli->query($query);            

        } catch (mysqli_sql_exception $e) { // Failed to connect? Lets see the exception details..
            echo "MySQLi Error Code: " . $e->getCode() . "<br />";
            echo "Exception Msg: " . $e->getMessage();
            exit; // exit and close connection.
        }

        $mysqli->close(); // finally, close the connection
 
        echo "Las metas han sido guardadas con éxito para ".$linea." - ".$genero;
  
 ?>  