<?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);
        
        $username = $inputdata['username'];
        $old_password = $inputdata['old_password'];
        $new_password = $inputdata['new_password'];
        $id = $inputdata['id'];
        
        $sel=$db->prepare('SELECT * FROM tbl_user WHERE id=?');
        $sel->execute(array($id));
        while($sels=$sel->fetch()) {
            
            if($old_password == $sels['password']) {
                $user=$db->prepare('UPDATE tbl_user SET username=?, password=? WHERE id=?');

                $req_user = $user->execute(array($username, $new_password, $id));
    
                if ($req_user) {
                    //echec
    
                    echo (json_encode(['response' => 1]));
    
    
    
                }else{
                    // réussie
    
                    echo (json_encode(['response' => 0]));
    
                    
    
                }
            } else {
                // ancien mot de passe incorrect
                echo (json_encode(['response' => -1]));
            }
            
            
        }


            


        