I am getting the following error when trying to update a record in an SQL database...
Book not updated: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Adobe AIR (Adobe Integrated Runtime) with Ajax: Visual QuickPro Guide, bookYear ' at line 1.
Here is my code for the updateBooks.php file
updateBook.php - updating a book record to a database using PDO
// Retrieve variables
$bookISBN = filter_has_var(INPUT_GET, 'bookISBN') ? $_GET['bookISBN'] : null;
$bookTitle = filter_has_var(INPUT_GET, 'bookTitle') ? $_GET['bookTitle'] : null;
$bookYear = filter_has_var(INPUT_GET, 'bookYear') ? $_GET['bookYear'] : null;
$catID = filter_has_var(INPUT_GET, 'catID') ? $_GET['catID'] : null;
$bookPrice = filter_has_var(INPUT_GET, 'bookPrice') ? $_GET['bookPrice'] : null;
$errors = false;
if (empty($bookISBN)) {
echo "You need to have selected a book.
\n";
$errors = true;
}
if (empty($bookTitle)) {
echo "You need to choose a title.
\n";
$errors = true;
}
if (empty($bookYear)) {
echo "You need to choose a year.
\n";
$errors = true;
}
if (empty($catID)) {
echo "You need to choose a category.
\n";
$errors = true;
}
if (empty($bookPrice)) {
echo "You need to choose a price.
\n";
$errors = true;
}
if ($errors === true) {
echo "Please try again.
\n";
}
else {
try {
//connects to database
require_once("functions.php");
$dbConn = getConnection();
$bookPrice = $dbConn->quote($bookPrice);
$updateSQL = "UPDATE nbc_books SET bookTitle = $ $bookTitle, bookYear = $bookYear, catID = $catID, bookPrice = $bookPrice WHERE bookISBN = $bookISBN";
$dbConn->exec($updateSQL);
echo "Book updated
\n";
} catch (Exception $e) {
echo "Book not updated: " . $e->getMessage() . "
\n";
}
}
?>
Here is my code for the bookQuery.php file
bookQuery.php - retrieving data from a database using PDO
All Books
try {
require_once("functions.php");
$dbConn = getConnection();
$sqlQuery = "SELECT bookTitle, catDesc, pubName
FROM nbc_books
INNER JOIN nbc_category
ON nbc_category.catID = nbc_books.catID
INNER JOIN nbc_publisher
ON nbc_publisher.pubID = nbc_books.pubID
ORDER BY bookTitle";
$queryResult = $dbConn->query($sqlQuery);
while ($rowObj = $queryResult->fetchObject()) {
echo "\n
{$rowObj->bookTitle}\n
{$rowObj->bookYear}\n
{$rowObj->catID}\n
{$rowObj->bookPrice}\n
\n";
}
}
catch (Exception $e){
echo "Query failed: ".$e->getMessage()."
\n";
}
?>
Here is the code for the form aswell
editBookForm.php- script to display an edit form for the chosen book
$bookISBN = filter_has_var(INPUT_GET, 'bookISBN') ? $_GET['bookISBN'] : null;
if (empty($bookISBN)) {
echo "Please choose a book.
\n";
}
else {
try {
require_once("functions.php");
$dbConn = getConnection();
$sqlQuery = "SELECT bookISBN, bookTitle, bookYear,nbc_books.pubID, nbc_books.catID, bookPrice
FROM nbc_books
INNER JOIN nbc_category
ON nbc_category.catID = nbc_books.catID
INNER JOIN nbc_publisher
ON nbc_publisher.pubID = nbc_books.pubID
WHERE bookISBN = $bookISBN";
$queryResult = $dbConn->query($sqlQuery);
$rowObj = $queryResult->fetchObject();
echo "
Update '{$rowObj->bookTitle}'
";
}
catch (Exception $e){
echo "Book details not found: ".$e->getMessage()."
\n";
}
}
?>
No comments:
Post a Comment