PHP MYSQL 下一行和上一行

标签 php mysql

基本上,我有一个包含数据的表格,还有一个包含框和内容的表单,我想做的是,当我单击下一个或上一个按钮时,它将从数据库中获取下一条记录。我可以做第一条和最后一条记录,但我无法弄清楚两者之间的关系。这就是我所拥有的。

$sql="SELECT * FROM Emails where ID > 1 ORDER BY UserEmail LIMIT 1";

      if ($result=mysqli_query($connection,$sql))
      {

      // Return the number of rows in result set
      $rowcount=mysqli_num_rows($result);

      while( $row = mysqli_fetch_array( $result ) )
      {
         $Email_Field         = $row['UserEmail'];         //primary key
         $Name_Field          = $row['UserName'];           
         $UserTel             = $row['UserTel'];         
         $Drop_Down           = $row['Drop_down'];      
         $MessageType         = $row['MessageType'];       
         $Comments            = $row['Comments'];        
         $SubjectOther        = $row['SubjectOther'];        
         $Check               = $row['Request'];         
      }

 <form method="POST" action="Controller_leads.php">
    <p><strong>What kind of comment would you like to send?</strong></p>
    <input type="radio" <?php if ($MessageType == "Complaint")   echo "checked"; ?> name="MessageType" value="Complaint">Complaint 
    <input type="radio" <?php if ($MessageType == "Problem")   echo "checked"; ?>  name="MessageType" value="Problem">Problem
    <input type="radio" <?php if ($MessageType == "Suggestion")   echo "checked"; ?>  name="MessageType" value="Suggestion">Suggestion
    <br> 
    <p><strong>What about us do you want to comment on?</strong></p>

    <select name="Drop_Down" size="1">
        <option value ="Web Site" <?php if ($Drop_Down == "Web Site") echo selected ?>>Web Site</option>
        <option value ="Office Hours" <?php if ($Drop_Down == "Office hours") echo selected ?>>Office Hours</option>
        <option value ="Pamphlet" <?php if ($Drop_Down == "Pamphlet") echo selected ?>>Pamphlet</option>
    </select>

    Other: <input type="text" size="26" maxlength="256" name="SubjectOther" value="<?php echo $SubjectOther ?>">

    <p><strong>Enter your comments in the space provided below:</strong></p>

    <textarea name="Comments" rows="5" cols="42"><?php echo $Comments;?></textarea><br><br>

    <strong>Tell us how to get in touch with you:</strong><br><br>

    <table>
      <tr><td width="45">&nbsp;Name     </td> <td><input type="text" size="35" maxlength="256" name="UserName" value="<?php echo $Name_Field ?> "></td></tr>
      <tr><td width="45">&nbsp;E-mail   </td> <td><input type="text" size="35" maxlength="256" name="UserEmail" value="<?php echo $Email_Field ?>"></td></tr>
      <tr><td width="45">&nbsp;Telephone</td> <td><input type="text" size="35" maxlength="256" name="UserTel" value="<?php echo $UserTel ?>"></td></tr>
    </table>

    <br>
    <input type="checkbox" name="Check" <?php if ($Check == "Contact Requested") echo checked; ?> value="Contact Requested">Please contact me as soon as possible regarding this matter
    <br><br> 
    <input type="submit" value="First" name="first"> 
    <input type="submit" value="Next" name="next"> 
    <input type="submit" value="Previous" name="previous"> 
    <input type="submit" value="Last" name="last"> code here

最佳答案

尝试向查询添加偏移量。每次单击时,$offset 都会加一,每次单击时,都会从 offset 中减去一。然后,在查询中包含偏移量,如下所示:

# get the current offset

# initial value
$offset = 1;

# if we have an offset from a previous or next click, use that
if (isset($_POST['offset'])) {

    # validate this input to protect against sql injection
    if (is_int($_POST['offset'])) {
        $offset = $_POST['offset'];
    }

    # now that we have our current value, see if we need to get the next or previous
    if ($_POST['submit']=="Next") {
        # next, add one offset
        $offset++;
    } else if ($_POST['submit']=="Previous") {
        # previous, go back one if we are greater than one
        if ($offset > 1) {
            $offset--;
        }
    }
}

# query time, give me one result (LIMIT 1), staring at record $offset 
$sql = "select SELECT * FROM Emails where UserEmail > 1 
        ORDER BY UserEmail LIMIT 1, $offset";

在您的表单中添加以下内容:

<input type="hidden" name="offset" value="<?php echo $offset; ?>">

另一方面,UserEmail > 1 看起来很奇怪,但我不知道你的数据。

关于PHP MYSQL 下一行和上一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703767/

相关文章:

mysql - 如何根据不同的列数据进行选择

php - 如何使用 PHP 仅检测 AUTO_INCREMENTed 值?

mysql - 从 ibdata1、ib_logfile0、ib_logfile1 和 .frm 文件恢复数据库

php - 优化ajax以减少mysql服务器负载

php - 在 php 应用程序中保持登录状态 +1 周

php - AJAX PHP 动态更新

php - 将下拉列表中的值从另一个表插入数据库

php - 在 Gii 上添加 where close 生成的 CRUD

javascript - 如何在模态弹出窗口的每次 Ajax 调用后重绘 Google Chart

php - 如果我在该图像中写入另一个变量,则图像标记在 php html 中不起作用。请找到下面所附的代码