Sometime this PHP code works and sometime don’t.. why?

I created a Php program in which a user will give his name and phone number and then the program will redirect to another Php file which is a preview page, after submitting it will go to the previous page and if everything is right it will show us ‘batch added successfully”, else it will show “duplicate value not possible”. Now yesterday this program worked, but today it is not working and fun fact, this same problem happened to my friend and i changed from $res = mysqli_query($conn, $sql); to $res = mysqli_query($conn,$sql); and it worked. But today both cases are not working in my program. Note : “phonenumber” is here UNIQUE in my “login” table and I am using XAMPP.

Code for pracaddtudent.php

<?php
include('connect1.php');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="pracaddstudentsave.php" method="POST">
        <input type="text" name="sname" placeholder="enter student name">
        <input type="text" name="phonenumber" placeholder="enter student phonenumber">

        <input type="submit" name="add" value="Submit">
        <input type="reset" name="reset" value="Reset">
    </form>
    <?php if (isset($_SESSION['add'])) {
    echo $_SESSION['add'];
    unset($_SESSION['add']);
} ?>
</body>
</html>

Code for pracaddstudentsave.php

<?php

include('connect1.php');

$sname = $_POST['sname'];
$phonenumber = $_POST['phonenumber'];



?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <div class="submit-form">
        <form action="" method="post">
          <input type="text" value=<?php echo $sname; ?> name="sname" readonly> 
          <input type="text" value=<?php echo $phonenumber; ?> name="phonenumber" readonly>
       
<input type="submit" name="save_student" value="Submit">
<input type="reset" name="reset" value="Reset">
        </form>
    </div>
</body>
</html>
<?php
    if (isset($_POST['save_student']) && $_POST['sname'] !="" ) {
       
        $sql = "insert into login set 
        sname='$sname',phonenumber='$phonenumber'";
        // $sql = "INSERT INTO login (sname, phonenumber) 
            // VALUES ('$sname', '$phonenumber')";
        $res = mysqli_query($conn,$sql);

        if ($res == true) {
            $_SESSION['add']= "Batch Added Successfully";
            header("Location:pracaddstudent.php");
            
        } else {
            
            $_SESSION['add']="Duplicate Value Not Possible";
            ?>
 <script> location.replace("pracaddstudent.php"); </script> 
            <?php
           echo mysqli_error($conn);
            
      }
}
?>

I tried it several times and most of the time it showed me “Duplicate value not possible” but yesterday same code worked. I was expecting it to work every time just like other web applications do. I also believe that there can be a problem about Session handling.