尝试使用PDO更新数据库中的记录时出现SQL错误

时间:2017-12-05 22:58:04

标签: php sql pdo

尝试更新SQL数据库中的记录时出现以下错误...

Book not updated:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在Ajax附近使用Adobe AIR(Adobe Integrated Runtime):Visual QuickPro Guide,bookYear'在第1行。

这是我的updateBooks.php文件的代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>updateBook.php - updating a book record to a database using PDO</title>
</head>
<body>

<?php
// 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 "<p>You need to have selected a book.</p>\n";
    $errors = true;
}
if (empty($bookTitle)) {
    echo "<p>You need to choose a title.</p>\n";
    $errors = true;
}
if (empty($bookYear)) {
    echo "<p>You need to choose a year.</p>\n";
    $errors = true;
}
if (empty($catID)) {
    echo "<p>You need to choose a category.</p>\n";
    $errors = true;
}
if (empty($bookPrice)) {
    echo "<p>You need to choose a price.</p>\n";
    $errors = true;
}
if ($errors === true) {
    echo "<p>Please try <a href='chooseBookList.php'>again</a>.</p>\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 "<p>Book updated</p>\n";
    } catch (Exception $e) {
        echo "<p>Book not updated: " . $e->getMessage() . "</p>\n";
    }
}
?>
</body>
</html>

这是bookQuery.php文件的代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>bookQuery.php - retrieving data from a database using PDO</title>
</head>
<body>
<h1>All Books</h1>
<?php
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 "<div class='book'>\n
                   <span class='bookTitle'>{$rowObj->bookTitle}</span>\n
                   <span class='bookYear'>{$rowObj->bookYear}</span>\n
                   <span class='catID'>{$rowObj->catID}</span>\n
                   <span class='bookPrice'>{$rowObj->bookPrice}</span>\n
              </div>\n";
    }
}
catch (Exception $e){
    echo "<p>Query failed: ".$e->getMessage()."</p>\n";
}
?>

</body>
</html>

以下是表单的代码以及

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>editBookForm.php- script to display an edit form for the chosen book</title>
</head>
<body>
<?php
$bookISBN = filter_has_var(INPUT_GET, 'bookISBN') ? $_GET['bookISBN'] : null;


if (empty($bookISBN)) {
    echo "<p>Please <a href='chooseBookList.php'>choose</a> a book.</p>\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 "
        <h1>Update '{$rowObj->bookTitle}'</h1>
        <form id='UpdateBook' action='updateBook.php' method='get'>
            <p>Book ISBN <input type='text' name='bookISBN' value='$bookISBN' readonly /></p>
            <p>Book Title <input type='text' name='bookTitle' size='50' value='{$rowObj->bookTitle}' /></p>
            <p>Book Year <input type='text' name='bookYear' size='10' value='{$rowObj->bookYear}' /></p>
            <p>Book Category <input type='text' name='catID' value='{$rowObj->catID}' /></p>
            <p>Book Price <input type='text' name='bookPrice' size='10' value='{$rowObj->bookPrice}' /></p>
            <br />

            <p><input type='submit' name='submit' value='Update Book'></p>
        </form>
        ";
    }
    catch (Exception $e){
        echo "<p>Book details not found: ".$e->getMessage()."</p>\n";
    }
}
?>
</body>
</html>

1 个答案:

答案 0 :(得分:-1)

您的查询语法无效:

$updateSQL = "UPDATE nbc_books SET bookTitle = $ $bookTitle, bookYear = $bookYear, catID = $catID, bookPrice = $bookPrice WHERE bookISBN = $bookISBN";

电除尘器。这部分:$ $bookTitle。删除多余的$