- <?php
- $servername = 'localhost';
- $username = 'root';
- $password = 'user@123';
- $dbname = 'publish';
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $title = $_POST["title"];
- $author = $_POST["author"];
- $edition = $_POST["edition"];
- $no_of_copies = $_POST["no_of_copies"];
- $sql = "SELECT * FROM books WHERE title = '$title' AND author = '$author' AND edition =
- '$edition'";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- $row = $result->fetch_assoc();
- $new_copies = $row["no_of_copies"] + $no_of_copies;
- $update_sql = "UPDATE books SET no_of_copies = '$new_copies' WHERE id = " .
- $row["id"];
- if ($conn->query($update_sql) === TRUE) {
- echo "Book details updated successfully.";
- } else {
- echo "Error updating book details: " . $conn->error;
- }
- } else {
- $insert_sql = "INSERT INTO books (title, author, edition, no_of_copies) VALUES ('$title',
- '$author', '$edition', '$no_of_copies')";
- if ($conn->query($insert_sql) === TRUE) {
- echo "New book inserted successfully.";
- } else {
- echo "Error inserting new book: " . $conn->error;
- }
- }
- }
- ?>
- <html>
- <head>
- <title>Insert Book Details</title>
- </head>
- <body>
- <h2>Insert Book Details</h2>
- <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
- Title: <input type="text" name="title" required><br><br>
- Author: <input type="text" name="author" required><br><br>
- Edition: <input type="text" name="edition" required><br><br>
- Number of Copies: <input type="number" name="no_of_copies" required><br><br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- <?php
- $conn->close();
- ?>