Native PHP, Form kept redirecting again to the same page when clicked submit

i have this practice project of mine that retrieved ID from ajax request, now it does retrieve the book with that ID, i have added a custom Code for the book to be inserted in the database but here’s the catch

After clicking the submit button, it redirect to the same page with the ID but now it does not display the ID and details of the book,

<?php
session_start();

// Check if the User_ID session variable is not set or empty
if (!isset($_SESSION["User_ID"]) || empty($_SESSION["User_ID"])) {
    // Redirect to index.php
    header("Location: ../index.php");
    exit(); // Ensure script execution stops after redirection
}


// Check if the ID parameter is set in the URL
if (isset($_GET['id'])) {
    // Get the ID value from the URL
    $id = $_GET['id'];


    // Database connection
    $conn = mysqli_connect("localhost", "root", "root", "db_library_2", 3308);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    // SQL query to retrieve available books
    $sql = "SELECT tbl_requestbooks.* FROM tbl_requestbooks WHERE Request_ID ='$id'";

    $result = $conn->query($sql);


    echo '<script>console.log("ID RETRIEVED '.$id.'");</script>';
} else {
    // If the ID parameter is not set in the URL
    echo "No ID parameter found in the URL.";
}

if (isset($_POST['submit'])) {
    echo '<script>console.log("Submit Function CLicked");</script>';
    

    $bookTitle = $_POST['Book_Title'];
    $pubname = $_POST['Publisher_Name'];
    $edition = $_POST['tb_edition'];
    $yr = $_POST['Year_Published'];
    $qty = $_POST['Quantity'];
    $price = $_POST['price'];
    $stat = "Pending";
    $country = $_POST['country'];
    $bibliography = "NA";
    $isbn = 1; // Assuming ISBN is always 1
    $sectionCode = $_POST["section"];
    $shelfNumber = $_POST["shelf"];

    $requestID = $_GET['id'];
    $authorsName = $_POST['Authors_ID'];


    // Retrieve the custom Accession Code
    $customAccessionCode = $_POST['accessionCode'];

    // Check if custom Accession Code is provided
    if (!empty($customAccessionCode)) {
        // Use the provided custom Accession Code
        $customAccessionCode = floatval($customAccessionCode);
    } else {
        // Generate a new random 6-digit value
        $randomValue = rand(100000, 999999); // Generate random value between 100000 and 999999
        $customAccessionCode = floatval($randomValue . '.2');
    }


  $checkDuplicateBookSql = "SELECT * FROM tbl_books WHERE Book_Title = '$bookTitle' AND tb_edition = '$edition'";
    $result = $conn->query($checkDuplicateBookSql);

    if ($result->num_rows > 0) {
        // Book already exists, update the quantity
        $row = $result->fetch_assoc();
        $existingQty = $row['Quantity'];
        $newQty = $existingQty + $qty;

        $updateQuantitySql = "UPDATE tbl_books SET Quantity = '$newQty' WHERE Book_Title = '$bookTitle' AND tb_edition = '$edition'";
        if ($conn->query($updateQuantitySql) !== TRUE) {
            echo '<script>console.log("Error updating book quantity: ' . $conn->error . '");</script>';
        }
    } else {
        // Check if the book already exists based on accession code
        $checkDuplicateAccessionCodeSql = "SELECT * FROM tbl_books WHERE Accession_Code = '$customAccessionCode'";
        $result = $conn->query($checkDuplicateAccessionCodeSql);

        if ($result->num_rows > 0) {
            // Accession Code duplicate detected, display message
            echo '<script>alert("Accession Code Duplicate Detected");</script>';
            echo '<script>console.log("Accession Code Duplicate Detected 99");</script>';
   
            // echo '<script>window.location.href = "process_data_book.php?id=' . $id . '";</script>';

       
        } else {
            // Insert the new book into tbl_books
            $booksql = "INSERT INTO tbl_books (Accession_Code ,Book_Title, Authors_ID, Publisher_Name, Section_Code, shelf, tb_edition, Year_Published, ISBN, Bibliography, Quantity, Price, tb_status) 
                        VALUES ('$customAccessionCode','$bookTitle', '$authorsID', '$pubname', '$sectionCode', '$shelfNumber', '$edition', '$yr', '$isbn', '$bibliography', '$qty', '$price', 'Available')";

            if ($conn->query($booksql) !== TRUE) {
                echo '<script>console.log("Error Inserting status 108");</script>';
                // echo '<script>window.location.href = "process_data_book.php?id=' . $id . '";</script>';
            }

            // Update tb_status based on Request_ID
            $updateStatusSql = "UPDATE tbl_requestbooks SET tb_status = 'Approved' WHERE Request_ID = '$requestID'";
            if ($conn->query($updateStatusSql) === TRUE) {
                echo '<script>alert("Record Updated Successfully!");</script>';
                echo '<script>window.location.href = "admin_books.php";</script>';
            } else {
                echo '<script>console.log("Error updating request status: ' . $conn->error . '");</script>';
            }
        }
    }
}


?>

this is the code that will first check if the book title and edition is the same, it will instead update the quantity of the record matching the user inputed

2nd is to check if the accesion_code is availalble or not, if the code is not avaiable then it will show error,

else it will insert the book and update the request table record,

this is the form

`

    <div class="mb-3">
            <label for="accessionCode" class="form-label">Custom Accession Code</label>
            <input type="text" class="form-control" id="accessionCode" name="accessionCode">
        </div>`

what should be the output is that when i click the submit btn i could just now insert the new book after the checks in my db, but because it instead redirect to the same page with the iD and i could not insert the new book

New contributor

Kurt Bernstein Blancia 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