<?php
session_start();
    header('Access-Control-Allow-Origin:*');
    header('Content-Type; application/json; charset=utf-8');
    header('Access-control-Allow-Method: POST');
    header('Access-control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-request-With');
    include('db.php');
        $inputdata = json_decode(file_get_contents("php://input"), true);
        
        $nom = $inputdata['nom'];
      
        $nbr="";
        
        // verifier si nom existant

        $unite_verif=$db->prepare('SELECT COUNT(id) AS nbr FROM repas WHERE nom=?');

        $unite_verif->execute(array($nom));

        while($unite_verifs=$unite_verif->fetch()){

            $nbr=$unite_verifs['nbr'];

        }

         if ($nbr == 0) {

            # inscription 

            $user=$db->prepare('INSERT INTO repas (nom) VALUES(?)');

            $req_user = $user->execute(array($nom));

            if ($req_user) {
                    //création réussie

                    echo (json_encode(['response' => 1]));



            }else{
                // création non réussie

                echo (json_encode(['response' => 0]));

                

            }

         }else {
            // email existe déjà

            echo (json_encode(['response' => -2]));

         }
        