javascript – Data is not saving into the mysql table-ThrowExceptions

Exception or error:

I am trying to save the current user’s id and the volume ID of a book generated by a search using google books API. What I have done is put the “card” for each book in a form, bringing in the volume id of the book which I will later hide. I then have a submit button which has the action ‘add.php’. This essentially should add the current user id and the volume id of the book into the database. Thanks.

add.php:

<?php



$errors = array(); 


// connect to the database
---> took this part out <---

//add the volume ID and user ID to the reading list

if (isset($_POST['add'])) {

$_SESSION['userid'] = $userid;
$volumeid = mysqli_real_escape_string($db, $_POST['volumeid']);

$query = "INSERT INTO readinglist (userid, volumeid) 
VALUES' ('$userid', '$volumeid')";

mysqli_query($db, $query);

header('location: index.php');       

}

?>

javascript.js:

$(document).on("mousedown", "button", function(){

  $("#button").on("click", function(){

      var search = document.getElementById('search').value
  document.getElementById('results').innerHTML = ""
   console.log(search);


    $.ajax({
    url: "https://www.googleapis.com/books/v1/volumes?q=" + search,
    success: function(json) {
      var htmlcontent = "";
      var thumb = "";
      var author = "";
      var description = "";


      for (i = 0; i < json.items.length; i++) {
        if (typeof json.items[i].volumeInfo.imageLinks != "undefined") {
          thumb = json.items[i].volumeInfo.imageLinks.smallThumbnail;
        } else {
          thumb = "http://i.imgur.com/oM3MdAi.png";
          //thumb = 'http://slems-oldboys.org.uk/library/wp-content/uploads/2013/11/library_nocover.jpg'
        }

         // title
        if (typeof json.items[i].volumeInfo.title != "undefined") {
          title = json.items[i].volumeInfo.title[0];
        }  


        // AUTHOR
        if (typeof json.items[i].volumeInfo.authors != "undefined") {
          author = json.items[i].volumeInfo.authors[0];
        }

             // DESCRIPTION
        if (typeof json.items[i].volumeInfo.description != "undefined") {
          description = json.items[i].volumeInfo.description;
        }

          //volumeid
          if (typeof json.items[i].id != "volumeid") {
          volumeid = json.items[i].id;
        } 


     htmlcontent +=

        "<div class='col-md-4'><div class='card'><div class='card-block'> <form method='post' action='add.php'><b> " + json.items[i].volumeInfo.title + '</b><center><p><img src="' + thumb + '" + alt="' + json.items[i].volumeInfo.title + '"></p></center>' + "<p><b>Author:</b> " + author + "</h4><br>" + "<p><b>Description: </b> " + trunc(description, 150) + "</p><input type='' id='volumeid' name='volumeid' value='" + json.items[i].id + "'><button class='btn-primary btn-sm' id='readmore'>More..</button><input type = 'submit' class='btn-primary btn-sm' id='add' value='Add to reading list' name='add'></div></div></div><br><br> ";
          }
        document.getElementById("results").innerHTML =
        "<div class='container'><h1>Your Search Results</h1><button class='btn-primary btn-sm' id='newest'>Order by Newest</button><button class='btn-primary btn-sm' id='relevance'>Order by Relevance</button><div class='row'>" + htmlcontent + " </form></div></div><br><br>";


    }
  });
});

function trunc(s, n) {
  var t = s.indexOf(" ", n);
  if (t == -1) return s;
  return s.substring(0, t) + "...";

}
})

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *