Why I can't Pass values to the database using php

I have built an ISBN generator using PHP. However, Once ISBN Number is generated it’s not stored in the database.

Here is the index.php

<form id="isbn-form" action="generate-isbn.php" method="POST">
  <label for="title">Book Title:</label><br>
  <input type="text" id="title" name="title"><br>
  <label for="author">Author:</label><br>
  <input type="text" id="author" name="author"><br>
  <button type="submit">Generate ISBN</button>

  const form = document.getElementById("isbn-form");
  form.addEventListener("submit", async (event) => {

    // Get the form data
    const formData = new FormData(form);

    // Send a POST request to the server
    const response = await fetch("generate-isbn.php", {
      method: "POST",
      body: formData,

    // Get the generated ISBN number
    const data = await response.json();
    const isbn = data.isbn;

    // Display the ISBN number
    alert(`Your ISBN number is: ${isbn}`);

Here is the isbn.php

function generate_unique_isbn() {
    // Generate a unique ISBN
    do {
      // Generate the first three digits (ISBN agency)
      $isbn = rand(100, 999) . "-";
      // Generate the language code
      $isbn .= rand(0, 9) . "-";
      // Generate the book identifier using a cryptographically secure random number generator
      $isbn .= bin2hex(random_bytes(4)) . "-";
      // Calculate the check digit
      $check_digit = 0;
      for ($i = 0; $i < strlen($isbn); $i++) {
        if ($i % 2 == 0) {
          $check_digit += (int)$isbn[$i];
        } else {
          $check_digit += 3 * (int)$isbn[$i];
      $check_digit = 10 - ($check_digit % 10);
      if ($check_digit == 10) {
        $check_digit = 0;
      $isbn .= $check_digit;
      // Connect to the database
      $conn = new mysqli("localhost", "root", "", "isbn");
      // Check if the ISBN is already in the database
      $result = $conn->query("SELECT * FROM books WHERE isbn = '$isbn'");
      // Close the database connection
    } while ($result->num_rows > 0);
    return $isbn;

Here is the generate-isbn.php


require "isbn.php";

header("Content-Type: application/json");

// Get the form data
$title = $_POST["title"];
$author = $_POST["author"];

// Generate a unique ISBN number
$isbn = generate_unique_isbn();

// Save the book to the database
$conn = new mysqli("localhost", "root", "", "isbn");
$conn->query("INSERT INTO books (isbn, title, author) VALUES ('$isbn', '$title', '$author')");

echo json_encode(["isbn" => $isbn]);

Database Structure

Why the values are not passing? Save the book to the database code in the generate-isbn.php. Values are generated but only one value is stored in the database. If I want to save the generated value in the database I have to delete the previous value that was stored in the database and run the script. Then it gets stored, After it doesn’t store then I have to re-run the process delete the value an run the code. What could be the issue?

>Solution :

The id column in your books table is primary key but you also have to set it AUTO_ICREMENT. Then you can insert rows more than one.

To do this you have to first click change action.

Change column attributes

Then check A_I checkbox in Structure panel.

Auto Increment

Leave a Reply