如何获得Id

时间:2014-02-27 15:41:01

标签: php jquery mysql

我不知道如何从我想要的项目中获取更新的ID。它是这样的。当我点击这个按钮

<button class="btn"><a href="bookUpdateUI.php?id=<?php echo urlencode($id);?>">Update<a />

我的页面会显示与所选项目的ID匹配的项目说明。在我的代码中,它无法识别id,而是显示已在db上列出的所有项目。它还给了我一个通知,未定义的索引。我做错了什么?我怎样才能做到这一点?

查询:

public function requestID($id,$table){

     $q="SELECT * FROM $table WHERE id = :id";
     $stmt = $this->con->prepare($q);
     $stmt->execute(array(':id'=>$id));
     $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
     return $result;
 }

bookUpdateUI.php:

    <?php
    session_start();
    include_once "../styles/header-menu-out.php"; 
    include "dbconnection.php";

    function __autoload($class){
    include_once("../main/".$class.".php");}
    $code = new codex;  
    try {
            $books = $code->requestID($_POST['id'], "book_info");
            $books = $code->showData("book_info");
    }catch (PDOException $e)
            {
                echo $e->getMessage();
            }

?>



<html>
<head></head>
<body>
    <form action="bookUpdate.php" method="POST" id="edit_form">
    <table>
    <?php foreach ($books as $book_info):?>
    <tr><td><input type="Hidden" name="id" value="<?php echo $book_info['id']; ?>"/></td></tr>
    <tr><td>Title</td><td><input type="text" name="title" value="<?php echo $book_info['title']; ?>"/></td></tr>
    <tr><td>Author</td><td><input type="text" name="author" value="<?php echo $book_info['author']; ?>"/></td></tr>
    <tr><td>ISBN</td><td><input type="text" name="isbn" value="<?php echo $book_info['isbn']; ?>"/></td></tr>
    <tr><td>Publisher</td><td><input type="text" name="publisher" value="<?php echo $book_info['publisher']; ?>"/></td></tr>
    <tr><td>Language</td><td><input type="text" name="language" value="<?php echo $book_info['language']; ?>"/></td></tr>
    <tr>
        <td>Genre</td>
        <td><select name="genre">
                    <option value="<?php echo $book_info['genre']; ?>"><?php echo $book_info['genre']; ?></option>
                    <option value="Non-Fiction">Non-Fiction</option>
                    <option value="Fiction">Fiction</option>
                    <option value="Educational">Educational</option>
                    <option value="Reserved">Reserved</option>
                    <option value="Instructional Materials">Instructional Materials</option>
            </select>
        </td>
    </tr>
    <tr><td>Quantity</td><td><input type="text" name="quantity" value="<?php echo $book_info['quantity']; ?>"/></td></tr>   

    <?php endforeach; ?>

    <tr>
        <td></td>
        <td>
        <input type="submit" value="Update" name="update"/>
        <a href="../bookDeleteUI.php"><input type="button" value="Cancel"/></a></td>
    </tr>
    </table>

    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

除了使用$books变量做奇怪的事情之外,因为URL(与表单不同)不是POST,它不会出现在$ _POST超级全局变量中。 (相反,您必须使用$_GET,或使用$_REQUEST超级全局覆盖两个基地。)

相关问题