I am sending some Google Book info to my db by retrieving JSON data. My code is below
$.ajax({
type: 'POST',
url: 'addIsbnScript.php',
data: {
'isbn' : isbn,
'title' : entry.volumeInfo.title,
'subtitle' : entry.volumeInfo.subtitle||'not available',
'author' : entry.volumeInfo.authors[0]||'not available',
'category' : entry.volumeInfo.categories ||'not available',
'description' : entry.volumeInfo.description ||'not available'
},
});
addIsbnScript.php
$title = $_POST['title'];
$category = $_POST['category'];
$author = $_POST['author'];
$isbn = $_POST['isbn'];
$subtitle = $_POST['subtitle'];
$description = $_POST['description'];
$query = $conn->prepare("INSERT INTO `book` (title,category,author,isbn,subtitle,description) VALUES (?,?,?,?,?,?)");
$query->bind_param('ssssss',
$title,
$category,
$author,
$isbn,
$subtitle,
$description
);
$query->execute();
Most of the time all of the data sends successfully, however I have noticed two things;
If there is more than one category in the JSON, the word 'Array' is sent to my db.
-
If there is no author present in the JSON my script doesn't run and I receive the following error in the console;
Uncaught TypeError: Cannot read property '0' of undefined
Very new to JS and PHP so any advice and direction is welcome.