代码之家  ›  专栏  ›  技术社区  ›  Second View

php Limit query pdo with button next(下一步)按钮

  •  2
  • Second View  · 技术社区  · 6 年前

    我的代码有什么问题。。我尝试单击“下一步”按钮以显示数据库中的下一条记录。。但当我点击时,什么都没发生。

    这是我获取记录的函数

    public function getBooks()
    {
         $limit = 1;
         $start=0;
    
        //SELECT loginUser.username, Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username="loay";
        $query = "SELECT Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username LIMIT $start, $limit";
        $statment = $this->db->prepare($query);
        $statment->execute([
            ':username' => $this->username
            //,':start' => $start, ':limit' => $limit
        ]);
        $result = $statment->fetchAll();
    
       echo "<table border='1'>
        <tr>
        <th>Books</th>
        </tr>";
        foreach($result as $row){
            echo "<tr>";
            echo "<td>" . $row['nameOfBook'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    

    }

    这是我在索引中的代码。php

      $object->getBooks();
    
      if( isset($_POST['next'])){
        $start +=1;
    }
    

    这是我的表单代码

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      Name: <input type="text" name="user" value="<?php echo $user;?>">
      <span class="error">* <?php echo $nameErr;?></span>
      <br><br>
      password: <input type="password" name="pass" value="<?php echo $password;?>" >
      <span class="error">* <?php echo $passwordErr;?></span>
      <br><br>
      <input type="submit" name="submit" value="Login">
      <br><br>
    
     
      <button type="button" name="button" onclick="window.location.href='/ooRigester.php'">SignUp</button>
      <br><br>
      <input type='submit' name='next' value='next' method="post"><br>
    </form>

    表单后的此代码

    <?php
    include_once('User.php');
    
    if(isset($_POST['submit'])){
    
    $username = $_POST["user"];
    $password = $_POST["pass"];
    
    $object = new User();
    $object->username= $username;
    $object->Password=$password;
    if( $object->isAuthenticated() ){
    
      echo "User Verified . <br><br>";
      $object->getBooks();
    
      if( isset($_POST['next'])){
        $start +=1;
        $object->getBooks($start); 
    }
    
    }
    
    else{
    
      echo "Wrong User Name Or Password . <br>";
    }}
    
    
    ?>
    

    这是在表单之前

    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    .error {color: #FF0000;}
    </style>
    <title>Login Form</title>
    </head>
    <body>
    <?php
    /*$str = "Hello";
    echo password_hash($str,1);*/
    // define variables and set to empty values
    $nameErr = "";
    $passwordErr = "";
    $user = "";
    $password = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["user"])) {
        $nameErr = "Name is required";
      } else {
        $user = test_input($_POST["user"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$user)) {
          $nameErr = "Only letters and white space allowed";
        }
      }
      if (empty($_POST["pass"])) {
        $passwordErr = "Password is required";
      } else {
        $password = test_input($_POST["pass"]);
      }
      
    }
    
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    ?>
    2 回复  |  直到 6 年前
        1
  •  2
  •   Karlo Kokkak    6 年前

    在里面 Index.php -您没有正确设置$起始值。以下代码集 $start 值来自 $_POST['start'] 如果不存在 $开始 默认值为0。

    而且您也没有正确接受表单提交。我修改了代码,通过对它们的$\u POST变量进行isset()检查来接受这两种提交类型(next和login)。

    使用:

    <?php
    include_once('User.php');
    
    if(isset($_POST['submit']) OR isset($_POST['next'])){
        $username = $_POST["user"];
        $password = $_POST["pass"];
    
        $object = new User();
        $object->username= $username;
        $object->Password=$password;
        if( $object->isAuthenticated() ){
    
          echo "User Verified . <br><br>";
    
          $start = 0;
          if(isset($_POST['start'])){
             $start = $_POST['start'];
          }
    
          $object->getBooks($start);
    
        }
    }
    else{
      echo "Wrong User Name Or Password . <br>";
    }
    
    
    ?>
    

    在表单代码中,必须添加一个隐藏输入,该输入保存下一条数据库记录的偏移量值。

    使用此代码:

    <form method="post" action="">
      Name: <input type="text" name="user" value="<?php echo $user;?>">
      <span class="error">* <?php echo $nameErr;?></span>
      <br><br>
      password: <input type="password" name="pass" value="<?php echo $password;?>" >
      <span class="error">* <?php echo $passwordErr;?></span>
      <br><br>
      <input type="submit" name="submit" value="Login">
      <br><br>
    
      <input type="hidden" value="<?php echo (intval(isset($_POST['start'])?$_POST['start']:0)+1);?>" name="start"/>
      <button type="button" name="button" onclick="window.location.href='/ooRigester.php'">SignUp</button>
      <br><br>
      <input type='submit' name='next' value='next' method="post"><br>
    </form>
    

    你的课堂方法 getBooks() 必须有 $开始 参数,以便它知道下一个要显示的记录。

    尝试:

    public function getBooks($start = 0)
    {
         $limit = 1;
    
        //SELECT loginUser.username, Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username="loay";
        $query = "SELECT Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username LIMIT $start, $limit";
        $statment = $this->db->prepare($query);
        $statment->execute([
            ':username' => $this->username
            //,':start' => $start, ':limit' => $limit
        ]);
        $result = $statment->fetchAll();
    
       echo "<table border='1'>
        <tr>
        <th>Books</th>
        </tr>";
        foreach($result as $row){
            echo "<tr>";
            echo "<td>" . $row['nameOfBook'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
        2
  •  2
  •   Tân Nguyễn    6 年前

    控制器中的代码:

    function getBooks($start = 0)
    {
        $limit = 1;
    
    //SELECT loginUser.username, Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username="loay";
        $query = "SELECT Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username LIMIT $start, $limit";
        $statment = $this->db->prepare($query);
        $statment->execute([
            ':username' => $this->username,
    //,':start' => $start, ':limit' => $limit
        ]);
        $result = $statment->fetchAll();
    
        echo "<table border='1'>
                <tr>
                    <th>Books</th>
                </tr>";
    
        foreach ($result as $row) {
            echo "<tr>";
            echo "<td>" . $row['nameOfBook'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    

    索引中的代码。php。

     $object->getBooks();
    
    if( isset($_POST['next']))
    {
        $start +=1;
        $object->getBooks($start);  
    }