Problem when inserting PHP data

0

This function that you have to do is insert the Menu, the SubMenu and Plates and insert it with their corresponding ids in a table called mapping that contains id_menu | id_sub_menu | id_plato. The problem is that when I insert it, I insert the plates in all the submenus.

if (filter_has_var(INPUT_POST, 'altaMenu')) {
    $menu->setNombre($_POST['nombreMenu']);
    $menu->setPrecio($_POST['precioMenu']);
    $id_menu = $crudMenu->insertar($menu);

    for ($i=0; $i < count($_POST['nombreSubMenu']); $i++) {
        print_r($_POST['nombreSubMenu'][$i]);
        $id_submenu = $crudMenu->insertarSubMenu($_POST['nombreSubMenu'][$i]);

        /*platos*/
        $array = $_POST['platos'];
        foreach ($array as $key) {
            //echo $key;
            $plato->setIdMenu('');
            $plato->setNombre($key);
            $plato->setPrecio('');
            $plato->setTipo('1');
            var_dump($plato);
            $id_plato = $crudPlato->insertar($plato);
            echo $id_plato;

            $crudMenu->insertMapSubMenu($id_menu,$id_submenu,$id_plato);   
        }
    }
    print '<br><b>Menu -> '.$id_menu.'. SubMenu -> '.$id_submenu.'. Plato -> '.$id_plato.'</b>';
    /**/
}

The problem that I am finding is that when inserting the data table by table I do not have any problem and I can show the data without problem, but if I use the lines of code above it only shows me the plates of a submenu .

 public function insertarSubMenu($nombre){
        $cnx = conexion::conectar();

        try{
            $sentencia = $cnx->prepare('INSERT INTO submenu VALUES(:id,:nombre)');
            $sentencia->execute(array(':id' => '',':nombre' => $nombre));
            return $cnx->lastInsertId();
        } catch(PDOException $e){
            echo 'Error -> '.$e->getMessage();
        }

    }



public function insertMapSubMenu($id_menu,$id_submenu,$id_plato){
        $cnx = conexion::conectar();

        $sentencia = $cnx->prepare('INSERT INTO map_submenu VALUES(:id,:id_submenu,:id_menu,:id_plato)');
        $sentencia->execute([':id' => '', ':id_submenu' => $id_submenu, ':id_menu' => $id_menu, ':id_plato' => $id_plato]);
    }

public function insertar($menu){
    $cnx = conexion::conectar();
    try{
        $sentencia = $cnx->prepare('INSERT INTO menu VALUES (:id,:nombre,:precio)');
        $sentencia->execute(array('id' => $menu->getIdMenu(),'nombre' => $menu->getNombre(),'precio' => $menu->getPrecio()));
        return $cnx->lastInsertId();
    } catch(PDOExceptio $e){
        echo 'Error con mensaje -> '.$e->getMessage();
    }
}
    
asked by sergibarca 21.05.2018 в 19:03
source

0 answers