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
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.