PHP PDO更新ID未动态传递

时间:2017-10-24 21:57:31

标签: php pdo

我正在尝试执行简单的更新CRUD操作,此功能仅在我的id是静态时才有效。例如,当我添加CreateProcess时,它可以正常工作。但是,当我尝试使用id = 1:id传递ID时,它将无效。但是,我没有收到错误。我缺少什么想法?我已经搜索了两天的答案。任何帮助,将不胜感激。我的代码如下。以下功能在我的圣经课程中。我在编辑页面上实例化了这样的对象。

id = ?

1 个答案:

答案 0 :(得分:1)

使用您的代码版本:

<?php
include('classes/config.php');
include('classes/scripture.php');

$scripture = new Scripture();

if (isset($_POST['edit'])) {
    // Validate id.
    if (!isset($_POST['id']) || empty($_POST['id'])) {
        echo 'Not a valid id. Please provide a valid one.';
    } else {
        $id = $_POST['id'];
        $book = $_POST['book'];
        $chapter = $_POST['chapter'];
        $verse = $_POST['verse'];
        $body = $_POST['body'];

        $affectedRows = $scripture->editScripture($id, $book, $chapter, $verse, $body);

        $message = 'Scripture successfully updated. Affected rows: ' . $affectedRows;
    }
} elseif (isset($_POST['create'])) {
    $book = $_POST['book'];
    $chapter = $_POST['chapter'];
    $verse = $_POST['verse'];
    $body = $_POST['body'];

    $lastInsertId = $scripture->createScripture($book, $chapter, $verse, $body);

    $message = 'Scripture successfully created. Id: ' . $lastInsertId;
}

class Scripture {

    public function createScripture($book, $chapter, $verse, $body) {
        global $db;

        try {
            $sql = "INSERT INTO scriptures (book, chapter, verse, body) VALUES (:book, :chapter, :verse, :body)";

            $bindings = [
                "book" => $book,
                "chapter" => $chapter,
                "verse" => $verse,
                "body" => $body,
            ];

            $query = $db->prepare($sql);
            $query->execute($bindings);
        } catch (PDOException $ex) {
            echo "An error occurred " . $ex->getMessage();
        }

        // Id of the last inserted row.
        return $db->lastInsertId();
    }

    public function editScripture($id, $book, $chapter, $verse, $body) {
        global $db;

        try {
            $sql = "UPDATE scriptures SET book = :book, chapter = :chapter, verse = :verse, body = :body WHERE id = :id";

            $bindings = [
                "book" => $book,
                "chapter" => $chapter,
                "verse" => $verse,
                "body" => $body,
                "id" => $id,
            ];

            $statement = $db->prepare($sql);
            $statement->execute($bindings);
        } catch (PDOException $ex) {
            echo "An error occurred " . $ex->getMessage();
        }

        // Number of affected rows.
        return $statement->rowCount();
    }

}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags must come first in the head -->

        <title>Demo</title>

        <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="stylesheet" />
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script>
    </head>
    <body>

        <form action="edit.php" method="POST">
            <input type="hidden" class="form-control" name="id" id="id" value="1">
            <div class="form-group">
                <label for="book">Book</label>
                <input type="text" class="form-control" name="book" id="book">
            </div>
            <div class="form-group">
                <label for="chapter">Chapter</label>
                <input type="text" class="form-control" name="chapter" id="chapter">
            </div>
            <div class="form-group">
                <label for="verse">Verse</label>
                <input type="text" class="form-control" name="verse" id="verse">
            </div>
            <div class="form-group">
                <label for="body">Body</label>
                <input type="text" class="form-control" name="body" id="body">
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-success" name="edit">
            </div>
        </form> 

    </body>
</html>