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.

New contributor

Dattatreya Dinda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật